0

I changed a wordpress theme on an old site and removed all plugins which were unnecessary.

All went well except for a shortcode that was used by the earlier author shows up on every post.

For example,

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu faucibus ipsum, id sagittis diam. Nam cursus ipsum quis dictum iaculis.

[spacer height=”10px”]

Quisque pellentesque risus risus, convallis consectetur odio malesuada non. Vivamus quis consequat diam. Nullam ac molestie purus. Vivamus id diam molestie, pulvinar urna eu, luctus purus. Sed ut cursus diam, sit amet tincidunt arcu. Integer blandit aliquam porttitor.

[spacer height=”5px”]

In consectetur dolor nulla, quis efficitur lectus tristique eu.

Is there a way I can remove this using the database on phpmyadmin. When I search the term it shows up in many places but I don't know more than this.

5 Answers 5

1

I love to use this tool, Search and Replace for WordPress Databases. It is basically a PHP script that is written for WordPress usage (but of course can be used with any database). When placed in the WordPress project, it automatically detects and loads the Database using data from wp-config.php file.

It enables you to make database queries based on regex, with a 'dry runs' - you will see a preview of the data that will be changed, but no real changes would be made. Also, it is serialization aware, meaning, if the variable you are replacing is inside a serialized object, it will safely be replaced, without breaking the serialization.

Setup is easy, just drop the folder in the WP root, and navigate the browser to the main PHP file. Remember to remove it afterwards.

I think you will love it as well, but remember to backup and double check before any live runs.

3

Site can be broken when you will update the content via SQL. I am giving a simple code and it will hide the [spacer height=”5px”] at front end.

Open the functions.php file of your current theme and add this code at end of the file.

add_shortcode( 'spacer','wse2019_remove_spacer' );
function wse2019_remove_spacer( $atts ) {
    return '';
}

It will automatically delete(basically it is hiding) the shortcode from your all existing posts/pages.

0

If you know which particular shortcode you want to remove and you want to remove it permanently from your database, you can simply do a SQL query with the command below:

UPDATE wp_post SET post_content = REPLACE(post_content, '[shortcodename]', '' );

Replace “shortcodename” with the shortcode you want to remove.

Note: This is not a foolproof method because different shortcodes can come with different attributes and value, making it hard to form a catch-all SQL query.

0

The simplest workaround I can think of is to define the shortcode and output an empty string. Add this code to functions.php of the current theme or in a custom plugin:

function wpse329827_ignore_spacer( $atts ) {
    return "";
}
add_shortcode( 'spacer', 'wpse329827_ignore_spacer' );

If you want to completely remove the shortcode, there are some plugins out there that can make search and replace queries for you like this and this.

If you have shell access to your server you can do the search-replace manually using wp search-replace command with --regex option.

Note that you should make a database backup before you do any replace operation.

1
  • 1
    add_shortcode( 'shortcodetag', '__return_false' ); seems even simpler
    – Chielt
    Commented Feb 27, 2020 at 12:33
0

With this small code-snippet you can remove all shortcodes from the db:

  1. Create a new file in the wordpress root dir
  2. Add the code below to the file
  3. Call the created file once

IMPORTANT:
This will remove all Shortcodes directly from the db - so make a backup before you execute the file!

<?php
require_once('wp-load.php');

global $wpdb;
$allPosts = $wpdb->get_results("SELECT * FROM `wp_posts`");
foreach($allPosts as $post){
  $content = RemoveShortcodes('[', ']', $post->post_content);
  $wpdb->update(
    'wp_posts',array('post_content' => $content),array( 'ID' => $post->ID )
  );
}

function RemoveShortcodes($beginning, $end, $string) {
  $beginningPos = strpos($string, $beginning);
  $endPos = strpos($string, $end);
  if ($beginningPos === false || $endPos === false) {return $string;}
  $textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
  return RemoveShortcodes($beginning, $end, str_replace($textToDelete, '', $string));
}
?>
1
  • 1
    What about escaped shortcode tags, like \[, or brackets that are just meant as normal text and not as shortcodes?
    – fuxia
    Commented Dec 4, 2020 at 0:59

Your Answer

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

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