• Resolved makeusawebsite

    (@makeusawebsite)


    Hi guys,
    We have a page on one of our websites, which is dynamic and populated via API calls to an external database.

    So in wordpress the PAGE is ‘tutor profile’, but if you go to tutor_profile/12345 the API call will obtain all information for ID 12345 and populated the page contents.

    So in wordpress, there is only ONE page (tutor profile) using a page template called tutor_profile. The page doesnt have any custom fields, but on the front end, there are many many pages, which populate text from the API.

    I can totally understand why, but every single tutor profile page has duplicate title and descriptions.

    Can anyone think of a way I could make these unique. I can see for example on our page template we are using variables such as $first_name, and I have tried referencing these in the TITLE using cf_first_name but it doesnt work…I guess because first_name isnt an actual custom field…its a variable in the template.

    Can anyone think of a way I could display variables which are referenced in the page template, and bring them into the title and description?

    Many thanks.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • It’s more elegant to you create CPT(custom post type) tutor-profile. On this way, you don’t need to override many stuff.

    If you want to create custom variable then you should use examples on https://gist.github.com/amboutwe/550c10ede7d065d9264930f5480ca748 Please notice that this filter runs before “content rendering”. So, you need to populate these variables before action wp_head.

    You can override meta description via filter wpseo_metadesc.

    Thread Starter makeusawebsite

    (@makeusawebsite)

    hi @stodorovic thank you for your help.

    Sadly the custom post type method isnt viable due to the api integration….but I wont bore you with that.

    I have looked over the solution you provided a link to, but I am struggling to see any outcome from it.

    The code snippets provided, I have tried placing into the page’s template file, before the get_header();line.

    However it doesnt make any difference….the title remains the same, and still comes from the one set in the back of wordpress.

    If you can confirm I am placing the code in the correct place it would be awesome.

    I think that it should work regardless that page templates aren’t perfect place for something like this.

    Other issue is probably because you need to set proper template which will cover all pages including your custom page (and other pages could have %%… in title). An option could be to change %%title%% (instead of adding new variable) which is already populated variable.

    Maybe, it’s better to you try to override complete title via filter wpseo_title (and other data with proper filter) before get_header in custom page template. It’s better solution if you want to keep everything in page template.

    Thread Starter makeusawebsite

    (@makeusawebsite)

    Thank you @stodorovic

    Ok I have made some progress.

    I have insert the replace code before the get_header, and it works.

    return ‘My name is Moses’;

    If I set the title as %%myname%% it correctly sets the title as “My name is moses”.

    The issue I have is I want to set the page title to be a variable (which is declared higher up the page).

    If I change the code to;

    return %tutorname;

    Do you have any idea how I would set the title to the content of the variable $tutorname?

    Many thanks

    The most elegant solution could be usage of PHP class (inside class should be filter and entire code to populate variable) . If you use procedural programming then I think that only way to exchange data ( between function and “main code” – template file ) could be global variable. It isn’t perfect because you should avoid “reserved names” in WP or other plugins,… ( $post, $posts, … – https://codex.wordpress.org/Global_Variables ). Anyway, I think that all variable in “template file” are globals (so, you should add some prefix to all variables in template file to avoid side effects).

    So, I’ll try to make concept how it could work. The code in functions.php:

    // define the custom replacement callback
    function get_tutorname() {
        global $tutorname;
    
        return isset( $tutorname ) ? $tutorname : '';
    }
    
    // Register tutorname variable
    add_action( 'wpseo_register_extra_replacements', function() {
        wpseo_register_var_replacement( '%%tutorname%%', 'get_tutorname', 'advanced', 'some help text' );
    } );
    

    In template file, you should set $tutorname before get_header():

    // Replace this line with code which populate $tutorname
    $tutorname = 'Tutor One';
    
    get_header();
    

    I didn’t test it, but I think that’s enough to you can make it to work.

    Thread Starter makeusawebsite

    (@makeusawebsite)

    @stodorovic Thank you so much for all your help.

    Just to confirm that I have managed to implement your suggested solution, and also done so for the meta-description. It works perfectly.

    I really appreciate your time, and if you are ever looking for work with wordpress please msg me, Id love to work with you.

    As the issues appears to be resolved we have closed this topic in order to keep the overview.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How To Get A Variable’ is closed to new replies.