0

I am creating a small plugin where I am creating a few shortcodes. I am using Classic Editor plugin.

Here is my code in Plugin.php:

<?php
/**
 * Plugin Name: Simple Data Visualizer
 * Description: Reads data from CSV and displays data in required format
 * Version: 1.0.0
 * Author: Subrata
 */

function evd_dreambig_shortcode() {
    return 'Hello! Can you dream big?';
}

function register_evd_shortcodes() {
    add_shortcode( 'dream-big', 'evd_dreambig_shortcode' );
}

add_action( 'init', 'register_evd_shortcodes' );

But when I put the shortcode in Post content as [dream-big], it is only rendering the tag, i.e. [dream-big] on post page rather than showing its content: Hello! Can you dream big?

I also took a different approach:

function evd_dreambig_shortcode() {
    return '<h1>Hello! Can you dream big?</h1>';
}

add_shortcode( 'dream-big', 'evd_dreambig_shortcode' );

But this does not work either.

I am using a custom theme. I also tried by adding add_filter( 'the_content', 'do_shortcode'); in functions.php. Still no luck!

plugin.php is the only file in my plugin directory.

What I am doing wrong?

  • Your exact code works perfectly for me. Is the plugin definitely active? Where are you placing the shortcode? In the visual editor? Does your theme template use the the_content() function? – Jacob Peattie Oct 2 '19 at 7:42
  • Would you please mind explaining a bit more? – Subrata Sarkar Oct 2 '19 at 9:39
  • Have you activated the plugin in Plugins > Installed Plugins? And where in WordPress are you typing the shortcode? I don't know what else you want me to explain. – Jacob Peattie Oct 2 '19 at 10:24
  • Oh yes! Of course I activated the plugin. And I am writing the shortcode in the editor (classic editor. not using block editor). The shortcode content is only coming after adding apply_filters( 'the_content', $post->post_content ); in single post page. Don;t don't why I am needing this. – Subrata Sarkar Oct 2 '19 at 10:54
  • 1
    Your template should be using the_content(), not $post or $post->post_content. Make sure you read the documentation on theme development: developer.wordpress.org/themes/basics/template-tags – Jacob Peattie Oct 2 '19 at 10:58
0

Try specifying the params defaults , like this:

evd_dreambig_shortcode($attr = [], $content = null){
    return $content
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.