WordPress – How to create a Widget

How to create a basic Widget?

<?php
/*
 * Plugin Name: Test Widget
 * Version: 0.1
 * Description: This is a Test Widget
 * Author: Me
 * Author URI: http://me.com
 */
?>
<?php
class TestWidget extends WP_Widget
{
    /*********************************************************************
     *    CONSTRUCTOR
     *********************************************************************/
    public function __construct()
    {
        parent::__construct(false, $name = 'This is a Test Widget');    
    } // __construct()
    

    /*********************************************************************
     *    DISPLAYS THE WIDGET
     *********************************************************************/
    function widget($args, $instance)
    {    
        extract($args);

        $html = 'Hey, this is a Test Widget!';

        echo $html;
    } // widget()
    
} // TestWidget

/*********************************************************************
 *    REGISTER THE WIDGET
 *********************************************************************/    
add_action('widgets_init', create_function('', 'return register_widget("TestWidget");'));
?>

WordPress – Disabling all Plugins using the Database

If you need to disable all plugins because one of the plugins is causing a problem and therefore you cannot get to the plugins page via the Dashboard (so-called “White Screen of Death”), you can disable all plugins directly in the database.

Locate the option name “active_plugins” (in the wp_options table)
Copy the value and paste it into some text editor (as a backup).
Change the value to:

a:0:{}

 


WordPress – Convert INNODB tables to MyISAM

How to convert all INNODB tables in the database to MyIsam?

<?php
    // CONVERT ALL INNODB TABLES TO MyISAM
    
    // connect your database here first 
    $sv_db_host     = '';
    $sv_db_user     = '';
    $sv_db_password = '';
    $sv_db_name     = '';

    $connect = mysql_connect($sv_db_host,$sv_db_user,$sv_db_password); 
    mysql_select_db ($sv_db_name);

    // Actual code starts here 

    $sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_SCHEMA = '".$sv_db_name."' 
        AND ENGINE = 'INNODB'";

    $rs = mysql_query($sql);

    while($row = mysql_fetch_array($rs))
    {
        $tbl = $row[0];
        echo "ALTER TABLE `$tbl` ENGINE=MyISAM;<br />";
        $sql = "ALTER TABLE `$tbl` ENGINE=MyISAM";
        mysql_query($sql);
    }
?>

Try this small WordPress plugin (InnoDB from / to MyISAM):
rvg-innodb-myisam


WordPress – Settings Link on Plugins Page

How to show a link to the plugin settings page on the main plugins page?

It will look like:
Settings | Deactivate | Edit

function MY_settings_link($links)
{ 
  $settings_link = '<a href="options-general.php?page=MY_SETTINGS_PAGE">Settings</a>'; 
  array_unshift($links, $settings_link); 
  return $links; 
}
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'MY_settings_link' );

Replace ‘MY_SETTINGS_PAGE‘ with the actual value for your plugin.


WordPress – Plugin Localization

How to localize a WordPress plugin?

1. Add the following code to the main plugin .php file:

/*****************************************************************************
    ADD THE LANGUAGE SUPPORT (LOCALIZATION)
******************************************************************************/
function my_action_init()
{   // TEXT DOMAIN
    load_plugin_textdomain('my-text-domain', false, dirname(plugin_basename(__FILE__)));
}
// INIT HOOK
add_action('init', 'my_action_init');

Important: ‘my-text-domain’ should be the same as the plugin name!

2. Put all the translatable text in the plugin inside of a function:

EXAMPLES

<?php echo 'WARNING'; ?>
becomes:
<?php echo __('WARNING', 'my-text-domain'); ?>
or:
<?php _e('WARNING', 'my-text-domain'); ?>

echo "<div class='test'>This is a test</div>";
becomes:
echo "<div class='test'>".__('This is a test','my-text-domain')."</div>";

$sort_txt = 'ascending';
becomes:
$sort_txt = __('ascending', 'my-text-domain');

3. Poedit

Download URL:
https://poedit.net/download

  • Run Poedit
  • Create an new catalog: file > new
  • Fill in the properties of the catalog, see screenshots

poedit_cat1

poedit_cat2

poedit_cat3

  • click the Update button to load the translatable text from the plugin source code
  • click the Save button to generate / update the .po and .mo files

NOTE:
Make sure the name of the .po file starts with the text domain, so it should be something like:
my-text-domain-nl_NL.po
and NOT just:
nl_NL.po


WordPress – Directories and URLs

Home page of the web site:

$siteurl = site_url('/');
   => https://web20bp.com/kb/

URL of the current theme:

$template_url = get_bloginfo(‘template_url’);
   => CAGE Knowledge Base

Current CHILD theme directory:

$template_dir = get_template_directory();
   => /home1/webtwoze/public_html/kb/wp-content/themes/twentyeleven

Current CHILD theme URL:

$template_url = get_template_directory_uri();
   => https://web20bp.com/kb/wp-content/themes/twentyeleven

Physical path to the current directory:

$file = dirname(__FILE__);
   => /home1/webtwoze/public_html/kb/wp-content/plugins/rvg-all-tags

Plugins URL:

$url = plugins_url();
    => http://localhost/wp-content/plugin

Plugins directory:

$dir = plugin_dir_path(__FILE__);
    => C:\Rolf\wp-content\plugins\rvg-float-to-top-button/

Load a javascript file:

wp_enqueue_script('fttb-script',
plugin_dir_url( __FILE__ ).'/js/jquery.scrollUp.min.js',
array('jquery'), $this->fttb_version, true);


WordPress – Create your own WordPress Plugin

This is a basic example of how to create your own WordPress plugin.

Create a file called my-plugin.php and save it to a new folder /wp-content/plugins/my-plugin:

<?php
/**
 * @package Show all the tags in a web site
 * @version 0.1
 */
/*
Plugin Name: Show all the tags used in a web site
Plugin URI: http://cagewebdev.com/
Description: Show all the tags, used in a web site
Author: Rolf van Gelder
Version: 0.1
Author URI: http://cagewebdev.com
*/
?>
<?php
function rvg_show_all_tags()
{   $arr = wp_tag_cloud('format=array&smallest=10&largest=10&number=0');
    $output  = '<h3>All Tags ('.count($arr).')</h3>';
    $output .= '<div style="border: solid 1px #ccc; padding:10px;">';
    for($i=0; $i<count($arr); $i++)
    {   if($i) $output .= ', ';
        $output .= $arr[$i];
    }
    $output .= '</div><br />';
    return $output;
}
add_shortcode('rvg-show-all-tags', 'rvg_show_all_tags');
?>

Note: don’t use the ‘echo’ command but store the output in a variable and return that variable!

See the plugin at work HERE!


WordPress – Adding a new plugin to the WP repository

How to add a new plugin to the WordPress plugin  repository?

Download and install subversion (svn) from:
http://subversion.apache.org/source-code.html

Ask for approval for the new plugin at:
http://wordpress.org/plugins/add/
(upload the zipped version of the plugin)

Directory structure for this example:

wordpress\wp-plugins
     my-new-plugin         (plugin sources)
     my-new-plugin-svn     (subversion data)

After being approved by WordPress (via email), upload the first version of the plugin by opening a command prompt and typing the following lines:

cd c:\wordpress\wp-plugins
svn co http://plugins.svn.wordpress.org/my-new-plugin my-new-plugin-svn
cd my-new-plugin-svn
svn up
copy ..\my-new-plugin\*.* trunk
rem also add subdirs if any!
svn add trunk/*
svn stat
svn cp trunk tags/0.1
svn ci -m "INITIAL RELEASE"

Leave the line ‘svn add trunk/*‘-line out when updating the plugin!

Resources:
http://digwp.com/2010/03/add-plugin-to-wordpress-plugin-repository/
http://wordpress.org/plugins/about/validator/
http://wordpress.org/plugins/about/svn/
http://codex.wordpress.org/Plugin_Submission_and_Promotion


WordPress – Adding HTML to the header

Add HTML to the header of a WordPress page / post using a plugin

/**
 * Load jQuery + a theme specific javascript file
 */
function kb_load_js()
{
    // A trick to get the child theme directory...
    $child_theme_url = get_stylesheet_uri();
    $child_theme_url = str_replace('/style.css', '', $child_theme_url);
    $output = '<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>';
    $output .= '<script type="text/javascript" src="'.$child_theme_url.'/kb_js.js"></script>';
    echo $output;
}
add_action('wp_head','kb_load_js')

Used in:
Rocket Reader plugin

Resources:
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head