Help with shortcode in admin-ajax [closed]

I’ve been playing around with ajax in wordpress and I’ve gotten my efforts to work on both back and front-end without too much trouble, however I’ve run into a problem getting post shortcodes to run when loading post_content to the front-end.

Here’s what I’ve noticed:

apply_filters( 'the_content', $content );

..will run but it only wraps my shortcode in <p> tags, the way it does in the admin.

do_shortcode() will work when using or other native wordpress shortcodes, but not the one i’ve registered in my plugin.

My plugin shortcode does however run in the normal wordpress post page (not ajax).

Having read about this, I’m aware that admin-ajax.php has WP_ADMIN set to true and so I think that what happening is that apply_filters( 'the_content', $content ); runs differently depending on whether is_admin or not.

Either that or native shortcodes and plugin shortcodes add_shortcode() are registered differently and not accessible in the same places.

Does anyone have any insight on what’s going on here?

Much appreciated.

1 Answer
1

How you are applying a filter is wrong. You need to bind a function to the filter, but you are providing a variable.

function my_filter( $content ) {
    return $content . 'My added code';
}
add_filter( 'the_content', 'my_filter' );

You should add the code like this, this should work.

Leave a Comment