filters - How to change dynamically page title according to variable data?

I created a shortcode for creating dynamic pages with the help of API data. If someone changes the page title I also wan

I created a shortcode for creating dynamic pages with the help of API data. If someone changes the page title I also want changes.

Now I have a dynamic page title in a shortcode function, but I need to run that variable in the 2nd function too.

1st Function Code:

$mainiid = $_GET['stattype'];
$title_new = $_GET['titype'];
$z123 = $title_new;

2nd Function code:

function custom_titles($z123) {

      global $wp;
      $current_slug = $wp->request;

      if ($current_slug == 'demo') {
        return $z123 . 'Foobar';
        print_r($z123);
      }
    }
    add_filter('wpseo_title', 'custom_titles', 10, 1);

I'm using Yoast plugin and my dynamic title was stored in var $z123 in the first function.

I created a shortcode for creating dynamic pages with the help of API data. If someone changes the page title I also want changes.

Now I have a dynamic page title in a shortcode function, but I need to run that variable in the 2nd function too.

1st Function Code:

$mainiid = $_GET['stattype'];
$title_new = $_GET['titype'];
$z123 = $title_new;

2nd Function code:

function custom_titles($z123) {

      global $wp;
      $current_slug = $wp->request;

      if ($current_slug == 'demo') {
        return $z123 . 'Foobar';
        print_r($z123);
      }
    }
    add_filter('wpseo_title', 'custom_titles', 10, 1);

I'm using Yoast plugin and my dynamic title was stored in var $z123 in the first function.

Share Improve this question edited May 5, 2019 at 11:57 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 5, 2019 at 11:38 M GRM GR 156 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Filters take variables from the function where they are defined. So, the Yoast plugin defines the wpseo_title filter inside on of its functions and passes a variable with it. You cannot insert your own variable.

However, since you are still in the same page build, you can still access the $_GET variables inside your own filter. So the trick would be to isolate the snippet from your first function in a third function, which you call from the second function as well. So, you don't pass the variable, but construct it twice, making sure in this way that the same value is available both times. Like this:

function function1() {
  // your code ...
  $z123 = function3();
  // your code ...
}

function function2( $title ) {
  // your code ...
  $z123 = function3 ();
  // your code ...
}
add_filter( 'wpseo_title', 'function2', 10, 1 );

function function3() {
  $mainiid = $_GET['stattype'];
  $title_new = $_GET['titype'];
  $z123 = $title_new;
  return $z123;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745521133a4631265.html

相关推荐

  • filters - How to change dynamically page title according to variable data?

    I created a shortcode for creating dynamic pages with the help of API data. If someone changes the page title I also wan

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信