0

I found this code to have a custom title on WordPress

add_filter( 'pre_get_document_title', 'myFilterTitle', 10, 1);

function myFilterTitle($myTitle) 
{    
    return "$myTitle here";
}

and some of my pages uses this shortcode to create content

function digitSix()
{
      //some codes here;

      $var = "this is custom title";
      apply_filters('pre_get_document_title', $var);

      return $someContent;
}

add_shortcode("i_digit6", "digitSix");

I was able to modify the title but it only gives "here" since it is a hard coded text inside the function myFilterTitle($myTitle)

It seems that the argument $myTitle I passed wasn't captured by the function.

2
  • your shortcode doesn't return content. try with return apply_filters('pre_get_document_title', $var);
    – mmm
    Commented Nov 11, 2017 at 11:34
  • 1
    You'll want add_filter, not apply_filter. But keep in mind that shortcodes are evaluated in the content, and the title has already been sent at that stage.
    – janh
    Commented Nov 11, 2017 at 11:34

1 Answer 1

0

This code solved my problem.

function digitSix()
{
      //some codes here;

      $var = "this is custom title";

      add_filter( 'pre_get_document_title', function( $var ) use ( $var ) {
            return $var;
        }, 20 );

      return $someContent;
}

add_shortcode("i_digit6", "digitSix");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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