How to return a string that has a variable inside in a shortcode?

When you want to return a data combined with a string, you can use one of the following methods: Either open and close the string using ‘: return ‘<span class=”class2″ id=”class3-‘ . $random . ‘”> </span>’; Or use the double quote: return “<span class=”class2″ id=’class3-{$random}’> </span>”; You can simply use $random without the {} curly … Read more

WP_Query in a shortcode

When you are constructing the $output variable, you need to consider get_the_post_thumbnail() get_the_excerpt() get_the_content() get_permalink() that return the values instead of the_post_thumbnail() the_excerpt() the_content() the_permalink() that echo the values.

Shortcode to Gutenberg-block: additional text on front-end and conditional display

Although a little different than I had initially anticipated, I have found/created a solution to my two problems. Conditionals I have opted to move non-required input to the sidebar, using the InspectorControls-element. Reason for this is that I realised that it would be impossible to hide a field (in the editor-part of the screen) that … Read more

Ajax not returning anything on form submit

Shortcodes are too late to add ajax actions. Additionally, that actions would be added only if shortcode is executed, which is very unlikely to happen on a ajax request. The quickest way to make your code work is to move the add_action outside addimage() function. add_action( ‘wp_ajax_wp_up’, ‘wp_up’ ); add_action( ‘wp_ajax_nopriv_wp_up’, ‘wp_up’); function wp_up() { … Read more

How to get shortcode to work inside a foreach loop called within a shortcode?

The issue is you are echoing raw post_content field from object. It isn’t what usually gets to front-end. Try: apply_filters( ‘the_content’, $post->post_content ) If you want to run shortcodes alone (without bunch of other filters) you can do: do_shortcode( $post->post_content ) Edit re-read your question… Is there a possibility that this thing gets recursive? Post … Read more

Format content value from DB outside of WordPress filters

There are a few filters and actions here that run. You’d need to look at the code and figure out what/how to utilize these. https://codex.wordpress.org/Function_Reference/wpautop https://codex.wordpress.org/Function_Reference/wptexturize https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content https://developer.wordpress.org/reference/functions/do_shortcode/ https://developer.wordpress.org/reference/functions/unescape_invalid_shortcodes/ https://developer.wordpress.org/reference/functions/get_shortcode_regex/

Get shortcode attributes outside shortcode function

You can pass an array to your script with wp_localize_script($handle, $object_name, $l10n). function test_function( $atts ) { $data = shortcode_atts( array ( ‘arrows’ => TRUE ), $atts ); wp_enqueue_script( ‘your_script_name’ ); wp_localize_script( ‘your_script_name’, ‘yourScriptObject’, $data ); return ‘a string’; } In your (external) script you can access the shortcode data now with … var arrows … Read more