Get current post id witout passing in shortcode

add_shortcode( ‘related-article’, ‘related_article_title’ ); function related_article_title( $atts ) { global $post; echo $post->ID; // currently viewing post id ob_start(); $query = new WP_Query( array( ‘post_type’ => ‘post’, ‘posts_per_page’ => 1, ‘order’ => ‘DESC’, ) ); if ( $query->have_posts() ) { ?> <div class=”menu-row”> <?php while ( $query->have_posts() ) : $query->the_post(); ?> Leggi anche: <a href=”https://wordpress.stackexchange.com/questions/210518/<?php … Read more

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

WordPress plugin shortcode not working

There are a couple of things to check here One: Is your plugin activated. Two: Is your shortcode in your main plugin file. If that code is in another file inside your plugin, did you make sure to include that extra file into your main plugin file Three: Don’t you have any type of error … Read more