-4

I am making some changes to an existing Wordpress theme.

public function checkTv( $post ) {
         global $title;
        if ( ! empty( $post['season'] ) ) { 
            $videourl ='shows'.$title. $post['season'].'-'. $post['episodio'];
        }

        return $videourl;
    }

Here the $videourl contains the desired URL format. Everything is working but the $title value is not being concatenated in the URL. It is being skipped automatically. In title i have the slug.

This how the call is being made

$postmeta = doo_postmeta_episodes($post_id);
$videourl = $this->checkTv( $postmeta );

$title has been declared as global and the value of the title is being taken from a function.

Robot
  • 1
  • 3
  • 3
    Where is the value of `$title` set? – Nigel Ren Mar 14 '19 at 07:57
  • you don't have `$title` variable in this method – Artem Ilchenko Mar 14 '19 at 07:57
  • Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Artem Ilchenko Mar 14 '19 at 07:58
  • _“$title has been declared as global”_ - no it has not, that would require the `global` keyword _inside_ your function. – 04FS Mar 14 '19 at 08:01

3 Answers3

2

PHP does not skip the $title variable: It is undefined in your function and therefore empty. To use a variable which is defined outside the function you need to put the global directive inside of your function:

public function checkTv($post) {
    global $title;
    //...
}

This informs the function that the $title you're about to use is the same as the one declared outside the function.

wortwart
  • 3,139
  • 27
  • 31
0

Try this:

$videourl = 'shows'
    . get_the_title($post) 
    . '-' 
    . $post['season'] 
    . '-' 
    . $post['episodio'];

UPDATE: Based on the comment that you stated you need slug instead of title, try this:

$slug = get_post_field('post_name', $post);
$videourl = 'shows' 
    . $slug
    . '-' 
    . $post['season'] 
    . '-' 
    . $post['episodio'];
0

Thanks for your help. I solved it. The Problem was with the scope but declaring it global was not solving it. I passed a another parameter with $post in the function that I got from $post_id and it solved the problem. thanks for your efforts

Robot
  • 1
  • 3