In a plugin I am creating, I need to change the output of the [caption]. I do this with (as from the Codex and other examples here)

add_filter('img_caption_shortcode', 'caption_fix', 10, 3);

function caption_fix ($x, $att, $content) {
   $content = 'new caption' ; // change it
return $content; }

But the [caption] ... [/caption] is still in the output; instead of "new caption" is expected to be in the output.

I have verified that the caption code is enabled with this code:

if (  shortcode_exists("caption")) { die("found caption shortcode"); }

And the generated page dies as expected.

Have carefully checked my caption_fix function to ensure that is returning content (if empty return $content, normal caption shortcode function is done).

This is with WP 4.8.1 and PHP > 5.3. (Added: WP 4.8.2 does same thing.)



(gotta read the docs a bit more carefully)

I was using this code in the loop

$xcontent =get_the_content();

But get_the_content() does not apply filters ... as the documentation states (if I was paying attention). I should use the_content(), or change it to this (which is the same as the_content()) :

$xcontent =get_the_content();
$xcontent = apply_filters('the_content', $xcontent);

So the issue was that filters were not being applied, since I used get_the_content() instead of the_content() .

That problem solved. Now on to the next one, with more careful reading of the Codex docs...

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.