Adding a class to shortcode API

If it really is just for one page you could use a conditional on the_ID(); from within your shortcode class (it is a custom shortcode right?) $class = the_ID() == 213 ? “some-other-class” : “”; $myhtml = sprintf(“<div class=”some-class %1$s”>%2$s</div>”, $class, $content); The other option is to add parameters or “attributes” to your shortcode as … Read more

Reload wordpress shortcodes

If you want to do this via AJAX, on the server side you could do something like the following: add_action( ‘wp_ajax_nopriv_update_shortcode_content’, ‘update_shortcode_content’ ); function update_shortcode_content(){ if( !empty( $_GET[‘new_shortcode_value’] ) ){ $updated_content = do_shortcode( ‘[theshortcode value=”‘ . $_GET[‘new_shortcode_value’] . ‘”]’ ); wp_send_json( array( “content” => $updated_content ) ); } }

Favorite websites with shortened hyperlinks

Sure, you’re looking for the shortcode API. That would enable you to write a short piece of code that does exactly what you want. As easy as this in your functions.php: function wpse246274_wiki( $atts ) { $link = “http://en.wikipedia.org/wiki/” . $atts[‘link’]; return $link; } add_shortcode( ‘wikilink’, ‘wpse246274_wiki’ ); Which you would use like this in … Read more

WordPress Shortcode function display outside of widget

I think the problem lies with functions like the_title() vs get_the_title() This seem to work: if (!function_exists(‘footer_get_latest_post’)) { function footer_get_latest_post() { $footerPost=””; $args = array(‘posts_per_page’ => 1 ); $recent_posts = new WP_Query($args); while( $recent_posts->have_posts() ) { $recent_posts->the_post(); $footerPost=”<div class=”footer-post-thumb”>” . get_the_post_thumbnail(‘thumbnail’) . ‘</div>’; $footerPost .= ‘<div class=”footer-post-content”><h3>’ . get_the_title() . ‘</h3>’; $footerPost .= get_the_content() . … Read more

Shortcode called twice

Shortcodes should never echo; they should always return the text to be displayed. See the User Contributed Notes in the add_shortcode() docs. Your code should read more like this: function displayTable() { $string = ”; $string .= ‘<table>’; $string .= ‘<tbody>’; $fields = get_field_objects(); foreach($fields as $field) { $string .= ‘<td>’; $string .= ‘<td>’. $field[‘label’] … Read more

Custom recent-posts shortcode’s thumbnail image not in line with rest of code

The problem is: the_post_thumbnail() outputs its content immediately. It’s basically like echoing instead of returning. To fix it, you should be able to just replace the_post_thumbnail(‘thumbnail’, [‘class’ => ‘droplet-img’, ‘title’ => get_the_title()]) with get_the_post_thumbnail(‘thumbnail’, [‘class’ => ‘droplet-img’, ‘title’ => get_the_title()])

What is @Action in WordPress?

This is called php annotations. Many of them are built-in via a standard called phpdoc (like @var), and they are used by the interpreter or the static tools (also by humans for a more structured comment). There is another usecase for these annotations, and it’s to run some code – probably using reflections (which usually … Read more

Shortcode attributes don’t appear?

You are return two times! The first return exit the function, so your shortcode outputs always “Hello World”. Correct code: function hello( $atts ) { extract( shortcode_atts( array( ‘foo’ => ‘something’, ‘bar’ => ‘something else’, ), $atts ) ); print_r($atts); // Remove, when you are fine with your atts! return ‘Hello, World: ‘.$atts[‘foo’]; } add_shortcode(‘hw’, … Read more