0

Is it possible to do this via WordPress Dashboard or PhpMyAdmin at once?

For example:

Find this: 
youtube.com/watch?v=ycbjSLCaMXw

Replace to this:
http://youtube.com/watch?v=ycbjSLCaMXw

There is hundreds of them.

Edit: I found this line on PhpMyAdmin but I don't know how to use it.

PMA Search Screen

And there is no "operator" dropdown in "Find and Replace" screen.

Find and Replace

2
  • WP CLI search replace would be the best option here, but is this just the post content field? Or are you using custom page builders or plugins like ACF that store things in post meta? – Tom J Nowell Jun 13 '20 at 14:02
  • @TomJNowell this is just for post content field. – Numan Çebi Jun 13 '20 at 19:40
0

I think you'll have to do a little PHP work and SQL-ing. As you might know, WordPress stores data in a database---and that database has multiple tables, each of which has several fields. You first have to know in what specific field(s) in which specific table(s) that youtube content resides. When you know where it is, then you can replace it. You can't search for content if you don't have some general idea where it is. For example, suppose you know that these URLs are in the post_content field of the posts table. That's where you'd look.

After you figure out where you want to look, then you can come up with a plan on how to do the work. I'll continue with my example where I am looking in the post_content field of the posts table. My plan includes ensuring that all my youtube strings are prefixed the same, so I'll have to do a search and replace over all records that contain a youtube.com/ string in their post_content field.

For each record, I will first remove all www prefixes from strings that contain //www.youtube.com/. This will make all my youtube strings look something like this: ...//youtube.com/.... Then, I will do the same thing on https:// prefixes by replacing them with http:// on all http://youtube.com/ strings. This will ensure that the only youtube strings that exist look something like this: http://youtube.com/...; and youtube.com/....

Next, my plan would include prefixing all youtube strings with http://. This will result in youtube strings looking something like this: http://http://youtube.com/...; and http://youtube.com/.... Not a big deal, because the next part of my plan calls for changing all the double http:// prefixes to just a single http:// making all my youtube strings look like this: http://youtube.com/.... Tada! (That's what you want, right?)

So, here's my "search-and-destroy" code for that. (I have described what each line does with a comment above that line of code so you can cross-reference it with my explanation above.)

// Get the object that can do WordPress database searches.
global $wpdb;

// Select records from the "posts" table where its "post_content" field contains "youtube.com/", and return all columns (*) for each of those records.
$results = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_content LIKE '%youtube.com/%' ");

// Iterate over each $record of the results
foreach($results as $record) {

    // Grab the string saved in post_content and put it in a variable we can manipulate.
    $str = $record->post_content;

    // Remove "www" from youtube strings
    $str = str_replace('www.youtube.com/', 'youtube.com/', $str);

    // Replace "https://" with "http://"
    $str = str_replace('https://youtube.com/', 'http://youtube.com/', $str);

    // Prepend the "http://"
    $str = str_replace('youtube.com/', 'http://youtube.com/', $str);

    // Replace the double "http://http://" with the single "http://"
    $str = str_replace('http://http://youtube.com/', 'http://youtube.com/', $str);

    // Save the manipulated string back into the database with the pretty youtube strings.
    $record->post_content = $str;
    wp_update_post(array(
        'ID'           => $record->ID,
        'post_content' => $str,
    ));

}

A final thought: if you put this code in your functions.php file of your template, it will run every time the file is included in the WordPress process, which is not ideal. So you should run the above code block once (by loading a page) then immediately remove (delete) the code block.

But, wouldn't it be nice to be able to just leave this code in your template somewhere and run it, say, once every 24-hours until the end of time without having to think about it (just in case more youtube strings need formatting in the future)? You could do this by putting the above code inside a hook which would initially run the code, then set a timer and run it again upon that timer's expiration? After all, it is expensive to query the database. If I were doing this, I would use the after_setup_theme hook and run the code once every 24-hours by setting a transient. Check this out:

// Whenever WordPress fires the "after_setup_theme" hook (which is pretty much on every page load), execute the block of code.
add_action('after_setup_theme', function() {

    // Only run our "search-and-destory" code if our transient expired
    if( !get_transient('_numanCebi_timer') ) {

        // Our "search-and-destroy" code goes here ...

        // Set a transient to expire after 24-hours so this code block is not entered again for a day.
        set_transient( '_numanCebi_timer', TRUE, DAY_IN_SECONDS );

    }

});

Update (6/14): The edits to your question are not WordPress related, and therefore are not germane in this forum; do not expect the updated question related to PhpMyAdmin to be answered here. Looks like what you found is a search tool in the PhpMyAdmin graphical user interface. As shown in your first screenshot, use % as a wildcard to search the records (which your screenshot shows you don't have in your youtube.com/ query). Next, there is no "operator" in a search and replace ("search-and-destroy") operation. It simply finds the string specified and replaces it with another specified string.

5
  • That's exactly what I want. But I have a question. Should I run your "search-and-destroy" code on PhpMyAdmin or functions.php? P.S. Since yesterday, there won't be youtube links without http:// preset. – Numan Çebi Jun 13 '20 at 19:34
  • PhpMyAdmin is a graphical user interface that allows you to manipulate the database by pointing and clicking a mouse. The code above is PHP code that needs to be executed directly on the server (as opposed to pointing and clicking). So, you'll have to make WordPress pull it in at run-time by placing it in a file that will be executed by the WordPress interpreter (like a template's functions.php file). By the way, if you like my answer, please up-vote it and mark it as the correct one. – Mort 1305 Jun 14 '20 at 1:00
  • I edited and add images to the question. Can you please check it out. – Numan Çebi Jun 14 '20 at 8:43
  • Updated my answer for you. – Mort 1305 Jun 14 '20 at 10:56
  • I used your plan with "Better Search Replace" plug-in and that's it. Problem solved! Almost 5k posts were edited successfully. – Numan Çebi Jun 14 '20 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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