Create a shortcode to display custom post types with a specific taxonomy

First off, it’s always good to register shortcode during init versus just in your general functions.php file. At the very least add_shortcode() should be in init. Anyway, let’s begin! Whenever you use add_shortcode() the first parameter is going to be the name of the shortcode and the 2nd will be the callback function. This means … Read more

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)