Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm new to MySQL so I don't know if this is even possible.

I would like to copy a url that's in post_content to a custom field. The url is variable and it looks like this:

[audio:http://www.website.com/folder/file.mp3]

Is it possible to copy just the url (http://www.website.com/folder/file.mp3) to a custom field?

I've tried different things but as I don't always understand how to query MySQL I've had no luck.

Thanks!

share|improve this question

1 Answer 1

What you need to do is run a function that gets triggered when the "update" button is clicked on posts.

I can't guarantee that my attempt to isolate the shortcode was successful, ( I'm not too good at that stuff ), but you definitely get the idea here!

add_action('save_post', 'save_details');
function save_details() {
    global $post;

    if(get_post_type($post->ID) == 'your_custom_post_type') {
      if(stripos($post->post_content, '[audio:http://') !== false) { // if shortcode exists
        $content = $post->post_content;

        //first position of shortcode
        $p1 = stripos($content, '[audio:http://');

        // strip code from before the shortcode.
        $content = substr($content, $p1);

        //find when end of the shortcode starts
        $p2 = stripos($content, '.mp3]');
        $p2 = $p2 + 5;  // add five to get where it ends.

        //get full shortcode
        $content = substr($content, 0, $p2);

        // save shortcode to meta field
        update_post_meta($post->ID, 'custom_field_name', $content);
    }
   }
}

Let me know if you have any troubles!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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