2

I have created a custom Plugin, and wanted to hide its shortcode [related] from the content once the plugin is deactivated. Everything is working fine, but when i deactivate the plugin the stray shortcode is still there. and i dont want to install any other plugin to hide that.

register_deactivation_hook( __FILE__ , array($relatedPost, 'deactivated'));
class RelatedPost {

function deactivated(){
    // flush rewrite rules
    flush_rewrite_rules();
}
}
3

I'm afraid hiding the shortcode once the plugin is deactivated is simply not possible.

The only way to "hide" a shortcode from the content is to register the shortcode with a calback that returns nothing, as in:

add_shortcode( 'related', function(){ return null; } );

But of course you cannot do that without an active plugin.

Technically you could remove every occurence of the shortcode by pulling and editing all of the content that may contain it - but that would probably be a very bad idea, and this wouldn't allow to get them back in case the plugin is reactivated.

  • what if i run your example code in register_deactivation_hook? will it work,if not why? thanks! – Kanon Chowdhury Feb 28 '18 at 9:40
  • No it won't, the deactivation hook only runs once during deactivation. add_shortcode() doesn't register the shortcode permanently, the call must be in an active plugin (or theme) to be effective. – Iceable Feb 28 '18 at 9:47
  • So you mean, there is no any way to hide the stray shortcodes ? – Awsme Sandy Mar 5 '18 at 7:59
  • Not unless you register them in another active plugin, or in the active theme, or you edit the content to remove or comment them directly in the DB (as in Max Yudin's answer). None of these options are ideal though. – Iceable Mar 5 '18 at 8:05
0

Following code will wrap [related] in HTML comment tags like <!-- [related] --> on plugin deactivation.

<?php
// on plugin activation
register_activation_hook( __FILE__, 'my_hide_shortcut' );

function my_hide_shortcut()
{
    $to_find = '[related]';
    $to_replace_with ='<!-- [related] -->';

    // get MySQL table prefix
    global $wpdb;
    $table_prefix = $wpdb->prefix;

    // run MySQL query
    $mysql = "UPDATE '{$table_prefix}_posts' SET 'post_content' = replace('post_content', '{$to_find}', '{$to_replace_with}')";
}

You can go further and do the opposite replace when plugin is activated again, or completely remove the shortcode on plugin uninstallation.

This code is not tested and was posted here only to suggest an idea! Make the database backup before you try. Check $mysql variable syntax. Also, may be you'll want to do MySQL stuff in WordPress way.

  • Comment tags sound better than removing it altogether as I mentionned in my reply. Though I'm not sure it would be wise to include such a potentially heavy DB query in a plugin if it is meant to be distributed. Of course it is fine if the plugin is for your own use and you safely keep a DB backup. – Iceable Feb 28 '18 at 9:00
  • Thanks, this one is good option, but that doesnt worked when i tried the same, and second thing is to reactivate the plugin will also needs to uncomment the shortcode which again use sql query, and that results in more chances in db errors. – Awsme Sandy Mar 5 '18 at 7:59
  • "doesnt worked" is undescriptive. And, as I've written in the answer, you can go further. On activation reverse $to_find and $to_replace_with values. The query does not change in general. – Max Yudin Mar 5 '18 at 8:14

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.