Shortcodes – are values defined by the user queryable?
-
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…
- The topic ‘Shortcodes – are values defined by the user queryable?’ is closed to new replies.