Shortcode Inside Class Not Working

if you use any hooks, filters or shortcodes with class methods, you have three options. either declare it in the context of an instance, in which case, you can refer to the instance of the class. class myClass { function __construct() { add_shortcode(‘my_shortcode_name’, [ $this, ‘shortcode’ ]); } function shortcode() { //your code here //properties … Read more

Wrap shortcode inside custom block

First Add a normal Shortcode block and type the shortcode [foo]123[/foo] Then click More Options for the block and select Add to Reusable Blocks Then provide a name as Foo Next, time you can search and add the Foo block instead of typing the shortcode Thanks

Executing a shortcode from a “normal” text/hyperlink

Yes and no I created a shortcode named foo: add_shortcode( ‘foo’, ‘fooshort’ ); function fooshort( $atts ) { return “https://google.com”; } Then put this in a post: test [foo] The result: test https://google.com So if I take your question literally, yes, you can put shortcodes in anchor attributes. But the shortcode has no idea it’s … Read more

Template part inside shortcode, unexpected reult

The get_template_part() function doesn’t assume any directory name, you have to explicitly supply it. It’s also relative to the active theme directory. So in many of your examples it was looking for the template in the root of your active theme. The correct format would be: get_template_part( ‘template-parts/test-one’ ); If you wanted to, you could … Read more

WordPress Code Standards and Working $link Parameters In Shortcode

how to make this shortcode [parent-child] working with link=false , and no extract in shortcode. You can never pass boolean value as shortcode parameter, rather it should be treated as string. Your comparison over the param link value false should be used as ‘false’ (string). add_shortcode( ‘parent-child’, ‘taxonomy_hierarchy’ ); function taxonomy_hierarchy( $atts ){ // Don’t … Read more

Display a post by ID attribute with shortcode

Try $HTML instead of $html Linux based systems are case-sensitive … function post_banner_shortcode($atts) { $atts = shortcode_atts( array( ‘id’ => ” ), $atts ); $post_id = $atts[‘id’]; $HTML = ‘<div class=”postbanner”>’; $HTML .= ‘<div class=”pb-thumb”>’ . get_the_post_thumbnail($post_id, ‘medium’) . ‘</div>’; $HTML .= ‘<div class=”pb-ttl-txt”>’; $HTML .= ‘<h4>’ . get_the_title($post_id) . ‘</h4>’; $HTML .= ‘<p>’ . … Read more

How to change page title (from a plugin) in twentytwentyone theme

You can do it like so: function filterDocumentTitle(string $title): string { // don’t change title on admin pages or when global $post is not loaded if (is_admin() || get_the_ID() === false) { return $title; } // don’t change title if shortcode is not present in content if (!has_shortcode(get_the_content(), ‘caption’)) { return $title; } return ‘special … Read more