Med fokus på din virksomheds forretningsgange watermark with eaktion circles

WordPress Plugin Shortcode Generator Not Saving Shortcodes

Recently I needed the WordPress plugin “Shortcode Generator” for one of my customers. Unfortunately I experienced the same as others: the plugin seems to work but when you go back and see your newly created shortcode, all you get is: “There are no generated shortcodes defined”.

With this post I publish a fix for the problem. I’ll do the same on some of the posts that report the problem on the WordPress website.

Why the plugin does not save codes

The reason is that the shortcode table in the database is not created when the plugin is activated. The table is not created because the table schema triggers an error due to one of the indexes being too large. You don’t see the error as WordPress suppresses it, however the table is not there.

To solve the problem you have to repeat the following steps, briefly put:

  • add code to properly deactivate the plugin
  • fix the database schema
  • deactivate the plugin
  • reactivate the plugin
I assume here that you have already tried and activated the plugin.

Detailed step-by-step description

These four steps are easy to accomplish even for non programmers, just follow the  description below:

Open the plugin main file in a text editor. The file is located inside the folder wp-content/plugins/shortcode-generator/ and the name is shortcode-generator.php.

Inside your WordPress admin section, you can edit it from

plugins>editor>Select plugin to edit >Shortcode Generator >shortcode-generator/shortcode-generator.php

Find the “uninstall” function at around line 65

function uninstall(){

}

And change it into this:

function uninstall(){

    delete_option('scg_version');

}

Find the “install” function at line 43:

function install(){
    global $wpdb;
    require(ABSPATH.'/wp-admin/includes/upgrade.php');
    $installed_version = get_option('scg_version');
    if($installed_version == '' || $installed_version < SCG_VERSION){
        $create_table = "
        CREATE TABLE {$wpdb->shortcodes} (
        `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        `shortcode` VARCHAR( 255 ) NOT NULL ,
        `value` LONGTEXT NOT NULL ,
        `type` VARCHAR( 255 ) NOT NULL ,
        UNIQUE (`shortcode`, `type`) ,
        FULLTEXT (
        `value`
        )
        )
        ";
        dbDelta($create_table);
    }
    add_option('scg_version',SCG_VERSION,'','no');
}

and change the definition of the “type” column to be 8 instead of 255. In this way Mysql will also have a better performance and since the types are only “wysiwyg” and “html”, both shorter than 8 chars, 8 is all you need.
The new “install” function looks like below:

function install(){
    global $wpdb;
    require(ABSPATH.'/wp-admin/includes/upgrade.php');
    $installed_version = get_option('scg_version');
    if($installed_version == '' || $installed_version < SCG_VERSION){
        $create_table = "
        CREATE TABLE {$wpdb->shortcodes} (
        `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        `shortcode` VARCHAR( 255 ) NOT NULL ,
        `value` LONGTEXT NOT NULL ,
        `type` VARCHAR( 8 ) NOT NULL ,
        UNIQUE (`shortcode`, `type`) ,
        FULLTEXT (
        `value`
        )
        )
        ";
        dbDelta($create_table);
    }
    add_option('scg_version',SCG_VERSION,'','no');
}

Then deactivate the plugin:

From your WordPress admin section

>plugins>installed plugins > Active

Click on “Deactivate”

Then reactivate

>plugins>installed plugins > Inactive

That’s it! The table should have been created and with it the ability to save your shortcodes.

Tags: , ,

Comments are closed.