0

I am trying to return an string that I can use in a function (programatically adding terms in WordPress).

My function that generates my string is basically looping through html meta tags that match a certain criteria and is as follows:

function getYouTubeTags( $post_id ) {

    $video_id = get_post_meta( get_the_ID(), 'rfvi_video_id', true );
    $tag_url = "http://www.youtube.com/watch?v=" . $video_id;
    $sites_html = file_get_contents($tag_url);
    $html = new DOMDocument();
    @$html->loadHTML($sites_html);
    $meta_og_tag = null;

    foreach( $html->getElementsByTagName('meta') as $meta ) {
        if( $meta->getAttribute('property')==='og:video:tag' ){
            $meta_og_tag = $meta->getAttribute('content');
            print_r ($meta_og_tag . ",");
        }
    }

}

When I simply execute this (getYouTubeTags();), it returns the string:

supra vs lambo,tt lambo,twin turbo,street race,texas streets,underground racing,supra,turbo supra,1200hp,nitrous,superleggera,gallardo,

In my function to add terms to a post, the following DOES NOT work:

function rct_save_post_terms( $post_id ) {

    $terms = getYouTubeTags();
    wp_set_post_terms( $post_id, $terms, 'post_tag', true );

}

If I manually add the string as outputted from the first function, it DOES work:

function rct_save_post_terms( $post_id ) {

    $terms = 'supra vs lambo,tt lambo,twin turbo,street race,texas streets,underground racing,supra,turbo supra,1200hp,nitrous,superleggera,gallardo,';
    wp_set_post_terms( $post_id, $terms, 'post_tag', true );

}

Also, according to WordPress, $terms in wp_set_post_terms: Can be an array or a comma separated string.

I know I must be missing something simple here but cannot seem to figure it out. Thank in advance for some help!

2 Answers 2

2

Since you want to get those string to be reused, why not return those:

function getYouTubeTags( $post_id ) {
    $out = null;

    $video_id = get_post_meta( get_the_ID(), 'rfvi_video_id', true );
    $tag_url = "http://www.youtube.com/watch?v=" . $video_id;
    $sites_html = file_get_contents($tag_url);
    $html = new DOMDocument();
    @$html->loadHTML($sites_html);
    $meta_og_tag = null;

    foreach( $html->getElementsByTagName('meta') as $meta ) {
        if( $meta->getAttribute('property')==='og:video:tag' ){
            // i seriously doubt this checking i think this should be
            // if($meta->getAttribute('property') == 'og:video') {
            $meta_og_tag = $meta->getAttribute('content');
            // print_r ($meta_og_tag . ",");
            $out[] = $meta_og_tag; // gather them inside first
        }
    }

    return implode(',', $out); // return implode comma delimited strings
}

And then utimately, then you could use them:

function rct_save_post_terms( $post_id ) {

    $terms = getYouTubeTags(); // strings are in here
    wp_set_post_terms( $post_id, $terms, 'post_tag', true );

}
Sign up to request clarification or add additional context in comments.

7 Comments

This worked perfectly. You have no idea how appreciative I am after struggling with this for a while. Thanks!
One quick question @Ghost: While this works perfectly, it seems to be having an issue running one multiple posts at the same time. I believe it is exhausting memory. Is there something that I can add to the end so each time it executes, it will flush the memory from the prior execution?
@JoeConlin how are you using this getYouTubeTags() function anyways? is this inside a loop? i think that culprit is that file_get_contents(), that really slows you down, strings inside the array i think is okay, anyway i do not know if you could explicitly tell PHP to free memory, i think this is automatic
I am hooking into the save_post action with add_action( 'save_post', 'rct_save_post_terms' ); so that any time a post is created, the function is run. The catch is that I am also importing/creating posts in bulk via a json response from the YouTube API so it is possibly trying to run multiple instances simultaneously. So, is there a way to ensure the this function will only run one at a time and ensure they dont run concurrently? I know this might have expanded beyond the scope of this question so my apologies.
@JoeConlin then i guess the only way to get around this is to find another route, since for every tag url, you will search for that meta tag and might not be very good. i haven't really used youtube api, but maybe there is a way of getting those info inside the meta tag thru the api instead of using file get contents and then getting thru dom, by the way, have you tried this? get_meta_tags(url), might be worth a shot
|
1

You don't seem to be returning a value in your original function. You need to use;

return $meta_og_tag;

at the end of your function to return a value back to an assigned variable.

Also, you need to append strings to the end of your returned variable using .=;

$meta_og_tag .= $meta->getAttribute('content');

OR you can save each attribute in an array and implode for the return;

// inside loop
$meta_og_tag[] = $meta->getAttribute('content');

// outside loop
return implode(', ',$meta_og_tag);

print_r will simply echo the contents of the variable, not return a value.

Hope this helps.

4 Comments

I tried using that but it only returns the FIRST value. I figured the return with stopping the loop??
You should place the return statement outside and after the loop. You also need to add the next value onto the string with .=, not replace it.
Instead of using print_r in the foreach loop, and knowing that return $meta_og_tag; only returns the first value, maybe someone can let me know how to return all values. That's probably where my issue is and unfortunately, I'm not that strong with foreach loops.
print_r doesn't return anything, it prints output only. Updated answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.