WordPress.org

Make WordPress Core

#42905 closed defect (bug) (invalid)

add_filter( 'the_title',<func>, 99 ); woudn't fire after add_shortcode

Reported by: strech Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.1
Component: Shortcodes Keywords: reporter-feedback
Focuses: Cc:

Description

I need to edit the title after add_shortcode was called. for the_content it works but not for the_title. This will be always fire after add_shortcode.

<?php
        add_shortcode("gallery_collection", array( $this, "show"));
        add_filter('the_content', array( $this, "replaceContent"),99);
        add_filter( 'the_title', array( $this, "replaceTitle"), 99 );

Change History (5)

#1 in reply to: ↑ description @strech
19 months ago

Replying to strech:

I need to edit the title after add_shortcode was called. for the_content it works but not for the_title. This will be always fire after add_shortcode.

<?php
        add_shortcode("gallery_collection", array( $this, "show"));
        add_filter('the_content', array( $this, "replaceContent"),99);
        add_filter( 'the_title', array( $this, "replaceTitle"), 99 );

Sorry i mean "This will be always fire BEFORE add_shortcode."

#2 @SergeyBiryukov
19 months ago

  • Component changed from General to Shortcodes
  • Keywords reporter-feedback added

Hi @strech, welcome to WordPress Trac! Thanks for the report.

Without looking at the show() method, it's not quite clear what the current and the expected results are. Could you please provide the full code to reproduce the issue?

#3 @dd32
19 months ago

This sounds like you're hoping to filter the_title based on data set by the shortcode callback?

If that's the case, the issue you're likely facing is that shortcodes are run at time of display, and in general, the title has already been output to the page at that point in time. However, as it's still processing the content, you can filter the post content from data set by the shortcode.

#4 @strech
19 months ago

@dd32 you are right

i want to clear the page and to only show "album content" if the url parameter "album" is set and the shortcode is loaded.

<?php
    function replaceContent($content) {
        if( $this->showAlbum) { // this works!  
            return $this->showAlbum($this->showAlbum);
        }else {
            return $content;
        }
    }

    function replaceTitle( $title ) {
        if( $this->showAlbum) { // always false
            return " Album ";
        }else {
            return $title;
        }
    }

    function show($atts){
        if (isset($_GET['album'])) {
            $this->showAlbum = $atts; // attrs are set!      
        }else {
            return $this->showCollection($atts);
        }
    }

Yes it's possible do this check erlier at the_content by usind "has_shortcode". But this will return true anyway also if the shortcode is hidden by a other shortcode

[hide] [myShortcode] [/hide]

Because of this i run the "add_shortcode" and if this was successfull i replace content and title.

So it's not possible to edit the title after "add_shortcode" ? Is there a timeline to learn which function has which live time? is there a other way?

#5 @dd32
19 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Thanks for confirming @strech

In this case then, it's working as expected, as the_title() has already output to the page at the point in time when the content is rendered and parsed.
If you were to change your template to display the title after the content of the post, then it'd work as you're hoping.

Your best bet is to not put the functionality into the shortcode callback, and instead process it earlier in the pageload somehow. You could also try pre-rendering the content by calling apply_filters('the_content', $post->post_content) before the title it output, but that'll mean the shortcode will run twice.

I'm closing this as invalid as WordPress isn't intended to work in the way you're hoping it would. Trac isn't for support questions, but the volunteers in the Support Forums may be able to assist.

Note: See TracTickets for help on using tickets.