Optimize shortcode callbacks

There is a very important programming principle: DRY – Don’t repeat yourself. Whenever you realize you are repeating almost the same job try to write an abstraction. For your pad* shortcodes this means: function get_padding( $atts ) { $args = shortcode_atts( array( ‘num’ => 10 ), $atts ); return str_repeat( ‘ ‘, (int) $args[‘num’] ); … Read more

Encode text string being appended as query to URL [closed]

To encode the URL, you could use the PHP urlencode( $url ) function or use the WordPress urlencode_deep( $array | $str ); function. add_shortcode( ‘dynamic_contact_button’, ‘button_product_page’ ); function button_product_page() { global $product; return urlencode( “https://wordpress.stackexchange.com/contact-form/?products=Product:%20” .$product->get_title(). “&#contact_form” ); } links: WordPress – urlencode_deep urlencode

Using wp_editor in shortcode

If a function echos data, you can use php output buffering to capture the echoed output and return it instead // Turn on the output buffer ob_start(); // Echo the editor to the buffer wp_editor(); // Store the contents of the buffer in a variable $editor_contents = ob_get_clean(); // Return the content you want to … Read more

Shortcode outputs at the top of the_content

All functions have to return a string, you should not use echo anywhere. Rewrite the functions, use an internal variable to handle the strings and return that: // Output a single menu item function projects_menu_entry($id, $title, $link_self) { global $blog_id; $out=””; if ($link_self || $id != $blog_id) { $out .= ‘<li>’; if ($id == $blog_id) … Read more

How does a shortcode work?

When using the_content(), WordPress will run several filters to process the text coming from the editor. These filters process the content before it is sent to the browser. do_shortcode is the filter that handles shortcodes. From /wp-includes/default-filters.php: // Shortcodes add_filter( ‘the_content’, ‘do_shortcode’, 11 ); // AFTER wpautop() do_shortcode() is used to search content for shortcodes … Read more

Shortcode empty attribute

There could be a couple ways to do this. Unfortunately, I don’t think any will result in exactly what you’re going for. ( [paragraph last] ) You could just create separate shortcodes for [paragraph_first] [paragraph_last] [paragraph_foobar] that handle $content without needing any attributes You could set the default value for last to false instead of … Read more

Using get_option() in JavaScript

Define an array of parameters to be injected into the script: $script_params = array( ‘myWidth’ => get_option(‘my_width’) ); Localize the script via wp_localize_script: wp_localize_script( ‘your-script-handle’, ‘scriptParams’, $script_params ); scriptParams now is a js object you can access from within the script: alert( scriptParams.myWidth ); // the value from the PHP get_option call in the js

Filter specific shortcode output?

There is no filter that I know of that is meant to target individual shortcodes like you want. You can filter attributes with shortcode_atts_{$shortcode} though. You can hijack another shortcode by creating your own callback and registering it with the original shortcode slug. I think that is what you are probably going to need to … Read more

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş