• Hiya,

    I just finished my first shortcode and I could be pretty happy now, but I’m not – as I’m not satisfied with the code and thought about asking you if a better version is possible.
    So. The shortcode creates a button. It has four parameters: text, title, width and position.
    My intention was to create a shortcode that produces different button styles and position, for example
    /a button positioned below the content block with some css and jquery trickery when user defines position=”outside”;
    /a button with normal, default positioning within the content block if user defines position=”inside”;
    /if the simplest form [button] is called without any parameter definition, a simple link is produced without any css formatting;

    Finally I only could realize a version which generates the button
    /inside of the content when no value for ‘position’ is defined;
    /outside of the content when ANY value for ‘position’ is defined;

    The actual code is:

    function linked_button($atts) {
    
        global $wpdb;
    
        extract(shortcode_atts(array(
            'title' => "contact",
            'text' => "Contact Us For More Information",
            'width' => "230",
            'position' => ""
        ), $atts));
    
        $page_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$title."'");
    
        if ($position) {
            $marginleft = (($width + 30) / 2);
            $url = get_permalink($page_id);
            return "<a href='$url' class=\"custombtn outside\" style=\"width:" . $width . "px; margin-left:-" . $marginleft . "px;\">$text</a>";
        }  else {
            $url = get_permalink($page_id);
            return "<a href='$url' class=\"custombtn inside\">$text</a>";
        }
    }
    add_shortcode('button', 'linked_button');

    What I tried was some attempt to use if statements, so instead of

    if ($position) {
            ...
        }  else {
            ...
        }

    querying the value by using

    if ($position='outside') {
            ...
        } elseif ($position='inside') {
            ...
        } else {
            ...
        }

    which didn’t and doesn’t work at all.
    Any ideas how to get the desired result?
    Thanks in adv…

Viewing 3 replies - 1 through 3 (of 3 total)
  • If the code shown above is actually what you tried, there is an issue with the way it is written. A single equal sign (=) is how values are assigned to variables in PHP (and JavaScript and a few other languages). To compare two values, you need to use a double equal sign (==) [or a triple equal sign if you want to perform a strict compare – but you wouldn’t be doing that in this case].

    Therefore, your last code block should look more like:

    if ($position=='outside') {
            ...
        } elseif ($position=='inside') {
            ...
        } else {
            ...
        }

    I’m not sure if that will fix the issue you’re having, but it’s a place to start.

    Thread Starter Gabor Lippert

    (@lunule)

    Hey Curtiss,

    Thanks – the issue you pointed out was The Issue. I rewrote the function with double equal signs, and now it works beautifully.

    Cheers!

    Awesome. Glad I could help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Shortcodes – are values defined by the user queryable?’ is closed to new replies.