admin管理员组

文章数量:1122832

I have this lovely bit of code that is running in a plugin, and generates a page using a shortcode.

...
   if(count($bb_job)>0){
            ?>
            <script type="text/javascript">
                let job_title = '<?php echo $bb_job -> job_title ?>';
                if(job_title) {
                    console.log('job title: ' + job_title)
                    // Change page title with job title
                    document.title = `Job Board: ${job_title} - Page Title`
                }
            </script>
            <div class="bbjob">
....

The code is basically just changing the document title using the available PHP variable 'bb_job'

What I would like to do is to change the actual HTML document title properly at the Wordpress level using something like this:

<?php 
                    function custom_document_title( $title_parts, $new_title ) {
                        $title_parts['title'] = $new_title;
                        return $title_parts;
                    }
                    
                    add_filter( 'document_title_parts', 'custom_document_title', 1000, 2 );

But when I try any of the available filters, nothing happens. I suspect I am trying to run the filter too late?

I only want to change this one single page title, so it reflects the content of the page when it is generated.

The page itself is a single wordpress page, with dynamic content.

I guess I could put a site wide filter in the theme functions.php file, and check if I am on the 'Job Board' page before changing the title, but then how can I pass in the new Job Title as a parameter?

I have this lovely bit of code that is running in a plugin, and generates a page using a shortcode.

...
   if(count($bb_job)>0){
            ?>
            <script type="text/javascript">
                let job_title = '<?php echo $bb_job -> job_title ?>';
                if(job_title) {
                    console.log('job title: ' + job_title)
                    // Change page title with job title
                    document.title = `Job Board: ${job_title} - Page Title`
                }
            </script>
            <div class="bbjob">
....

The code is basically just changing the document title using the available PHP variable 'bb_job'

What I would like to do is to change the actual HTML document title properly at the Wordpress level using something like this:

<?php 
                    function custom_document_title( $title_parts, $new_title ) {
                        $title_parts['title'] = $new_title;
                        return $title_parts;
                    }
                    
                    add_filter( 'document_title_parts', 'custom_document_title', 1000, 2 );

But when I try any of the available filters, nothing happens. I suspect I am trying to run the filter too late?

I only want to change this one single page title, so it reflects the content of the page when it is generated.

The page itself is a single wordpress page, with dynamic content.

I guess I could put a site wide filter in the theme functions.php file, and check if I am on the 'Job Board' page before changing the title, but then how can I pass in the new Job Title as a parameter?

Share Improve this question asked Sep 27, 2024 at 1:29 atlas_scoffedatlas_scoffed 1011 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 1

It seems that the way you attached your function to the document_title_parts filter is wrong. As per WordPress documentation, document_title_parts only accepts one parameters, array $title. So it doesn't make sense that when you have 2 parameters in your function.

I suggest you do something like this:

function custom_document_title( $title_parts ) {
    // Here is where you need to get the $bb_job variable
    $bb_job = null; // Change this to the way you use to obtain $bb_job

    // If $bb_job is not empty, assign $bb_job -> job_title
    if($bb_job && $bb_job->job_title){
        $title_parts['title'] = $bb_job->job_title;
    }
    return $title_parts;
}
add_filter( 'document_title_parts', 'custom_document_title', 1000, 1 );

本文标签: Changing a page title while the page is being built