WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Mar 01 2014

Plugin Uninstall: How to Delete Terms & Taxonomies from WordPress Database

Many developers do not consider what they should do when a person deactivates a plugin (like flush_rewrite_rules()), much less what to do with the data the plugin created, much of which is useless. In my opinion, this is partly because creating an extra uninstall.php file is a bit problematic to test over and over.

So, in this snippet, here is how you can delete ALL the terms and the associated taxonomy when a plugin is deleted by a user. First create a file called uninstall.php which appears at the root of the plugin. Then add this code:

<?php
/** Delete All the Taxonomies */
foreach ( array( 'my_first_custom_tax', 'my_second_custom_tax', 'my_third_custom_tax', ) as $taxonomy ) {
// Prepare & excecute SQL, Delete Terms
$wpdb->get_results( $wpdb->prepare( "DELETE t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('%s')", $taxonomy ) );
// Delete Taxonomy
$wpdb->delete( $wpdb->term_taxonomy, array( 'taxonomy' => $taxonomy ), array( '%s' ) );
}
view raw uninstall-terms-taxonomy-1.php hosted with ❤ by GitHub

Sometimes, however, you may need to do something else between the code. For example, you may have custom term meta within the options table that would need to be deleted as well.

<?php
/** Delete All the Taxonomies */
foreach ( array( 'my_first_custom_tax', 'my_second_custom_tax', 'my_third_custom_tax', ) as $taxonomy ) {
// Prepare & excecute SQL
$terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('%s') ORDER BY t.name ASC", $taxonomy ) );
// Delete Terms
if ( $terms ) {
foreach ( $terms as $term ) {
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $term->term_taxonomy_id ) );
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term->term_id ) );
delete_option( 'prefix_' . $taxonomy->slug . '_option_name' );
}
}
// Delete Taxonomy
$wpdb->delete( $wpdb->term_taxonomy, array( 'taxonomy' => $taxonomy ), array( '%s' ) );
}
view raw uninstall-terms-taxonomy-2.php hosted with ❤ by GitHub

Written by Travis Smith · Categorized: WordPress

Feb 26 2014

Beginner’s Guide to Google Webmaster Tools: 12 Ways to Grow Your Blog

Google Webmaster Tools How To

Written by Travis Smith · Categorized: WordPress

Feb 25 2014

Deactivate Your Plugin Extension with the Extended Plugin

In the previous post, Extending or Modifying a Plugin, in this two part series, I wrote about modifying by extending an existing plugin using a plugin. This left us with the following question:

How do I deactivate my plugin if the extended plugin was deactivated?

This snippet will allow you to deactivate your extended plugin when the extended plugin deactivates.

<?php
add_action( 'update_option_active_sitewide_plugins', 'dpse_deactivate_self', 10, 2 );
add_action( 'update_option_active_plugins', 'dpse_deactivate_self', 10, 2 );
/**
* Deactivate ourself if Addthis is deactivated.
*/
function dpse_deactivate_self( $plugin, $network_deactivating ) {
if ( !is_plugin_active( 'addthis/addthis_social_widget.php' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ), true );
}
}
view raw display-posts-deactivate.php hosted with ❤ by GitHub

Picking up from the previous post, in the example below, there is a code snippet to customize Display Posts Shortcode. So, if the client decides to deactivate Display Posts Shortcode, then this plugin will deactivate itself automagically.

Yes, it is strange that I am hooking into the updated_option hook. One would think that this could be done with the deactivated_plugin hook, but it cannot since at the time of this post either the documentation (ticket #27189) is wrong or the action occurs at the wrong place (ticket #27189, see also #20241).

Full Extended Plugin

<?php
/**
* Display Posts Shortcode Extension Plugin
*
* @package DT_Display_Posts
* @author Travis Smith <[email protected]>
* @license GPL-2.0+
* @link http://wpsmith.net
* @link http://wordpress.org/plugins/display-posts-shortcode/
* @copyright 2014 Travis Smith
*
* @wordpress-plugin
* Plugin Name: Display Posts Shortcode - My Extension
* Plugin URI: http://www.wpsmith.net
* Description: Extends Display Posts Shortcode
* Version: 1.0.0
* Author: Travis Smith
* Author URI: http://www.wpsmith.net
* Text Domain: displayposts-ext
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
*/
add_filter( 'display_posts_shortcode_args', 'dpse_shortcode_exclude_posts', 10, 2 );
/**
* Add "not_in" arg to Display Posts Shortcode
*
* @since 1.0.0
*
* @param array $args Default Display Posts Shortcode args.
* @param array $atts Original shortcode attributes.
* @return array $args Modified Display Posts Shortcode args.
*/
function dpse_shortcode_exclude_posts( $args, $atts ) {
if( isset( $atts['not_in'] ) ) {
$args['post__not_in'] = explode( ',', $atts['not_in'] );
}
return $args;
}
add_action( 'update_option_active_sitewide_plugins', 'dpse_deactivate_self', 10, 2 );
add_action( 'update_option_active_plugins', 'dpse_deactivate_self', 10, 2 );
/**
* Deactivate ourself if Addthis is deactivated.
*/
function dpse_deactivate_self( $plugin, $network_deactivating ) {
if ( !is_plugin_active( 'display-posts-shortcode/display-posts-shortcode.php' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ), true );
}
}
view raw display-posts-shortcode-ext-plugin.php hosted with ❤ by GitHub

Written by Travis Smith · Categorized: WordPress

Feb 24 2014

Beginner Developer Series: Computer Softwares

As part of a computer setup, there are some essential programs one needs to have installed. Personally, here is my suite of programs that I install on a clean machine.

First and foremost, there is an awesome site called Ninite that helps setup Windows machines. From Ninite, I always install the following.
Software: Ninite

  • Web Browsers
    • Chrome
    • Opera
    • Firefox
  • Utilities: WinDirStat (helps find large files)
  • Messaging
    • Skype
    • Thunderbird (free email client, alt. to MS Outlook)
  • Media
    • iTunes (music/video)
    • VLC (video player, plays everything)
    • Audacity (audio editing)
    • Spotify (better radio than iTunes)
    • QuickTime
  • Runtimes *REQUIRED
    • Java
    • .NET
    • Silverlight
    • Air
    • Shockwave
  • Compression: 7-zip (helps find large files)
  • Imaging
    • GIMP (optional, if you do not have PhotoShop)
    • Inkscape (optional, if you do not have Illustrator)
  • Documents
    • OpenOffice (optional, free alternative to MS Office)
    • Reader
  • Online Storage
    • Dropbox
    • Google Drive
    • SkyDrive
  • Developer Tools
    • Python (optional)
    • Filezilla (FTP/sFTP)
    • Notepad++ (Powerful Editor)
    • JDK (optional)
    • PuTTY (required for Git, etc.)
    • Eclipse (optional IDE)

Once you select all of these, you simply click "Get Installer," then run the installer, and go get some Starbucks for Brian Gardner's sake or a Mountain Dew for my sake. Simply walk away and do something else for a bit.

After these install, I also install the following softwares manually:

  • Adobe Creative Suite/Cloud Master Collection (par excellence design tool)
  • Latest Version of Internet Explorer
  • Safari for Windows
  • XAMPP: Local Apache & PHP server for rapid development.
  • MySQL Workbench: If you are used to MS SQL Server, then this will make you feel more at home.
  • Git for Windows (Download): For running Git from the command line.
  • TortoiseSVN: WordPress Core Trac Patches & use with Assembla SVN (free private SVN repos)
  • TortoiseGit: Use with Github & Bitbucket (free private Git repos)
  • Atlassian Source Tree: Windows GUI for Git (works with Github and Bitbucket)
  • Bitvise Tunnelier: SSH & FTP Client, better than PuTTY IMHO
  • NodeJS: Use with CSSLint, JSLint, Grunt, etc.
  • Git Credentials: Stores credentials so I don't have to enter them over and over again.
  • Screen Capturing Software: Jing and/or TinyGrab2
  • Cyberduck: Awesome FTP client for Amazon S3
  • Sublime Text: Premium Editor ($70)
  • phpStorm: Premium Editor ($99)

Did I miss anything from a Windows perspective?

Written by Travis Smith · Categorized: WordPress

Feb 23 2014

Extending or Modifying a Plugin

Many times, I extend plugins with additional functionality or customize plugins with specific needs of the theme or the site. You may do the same, but where does this code need to go? Should it go in functions.php or should it reside in its own plugin?

Before I answer this question, let's consider these questions.

  1. How do you handle troubleshooting on plugin updates if the theme or site breaks?
  2. Since the original functionality exists in a theme, what happens to that functionality if the user switches themes? Or, in other words, is this customization truly theme specific?
  3. What happens to the code if the extended plugin is deactivated?

Depending on how you answer these questions, I would say whether the extension should be in either it's own plugin or in the theme's functions.php though one could easily argue that regardless, it is plugin territory, especially in light of Thomas Griffin's Plugin Activation class.

For example, there are times when plugins release an update and this update either breaks your theme or breaks your extension. The former is going to happen periodically with some plugins, but if the latter happens and your extended code resides in functions.php, then your theme may break. Regardless, it becomes an effort to determine where the breakdown occurs: is it the theme or the extension? So, if it resides in functions.php, we go and comment the extension to see if it was the problem.

However, if the extension lives within a plugin, then it is just a simple test by deactivating the plugin to determine whether it was the problem. Also, having it reside in a plugin keeps the functions.php file cleaner.

Now, if you extend a plugin that contains a shortcode, like Display Posts Shortcode. So for example, let's say we add an attribute to the display posts shortcode.

<?php
add_filter( 'display_posts_shortcode_args', 'dpse_shortcode_exclude_posts', 10, 2 );
/**
* Add "not_in" arg to Display Posts Shortcode
*
* @since 1.0.0
*
* @param array $args Default Display Posts Shortcode args.
* @param array $atts Original shortcode attributes.
* @return array $args Modified Display Posts Shortcode args.
*/
function dpse_shortcode_exclude_posts( $args, $atts ) {
if( isset( $atts['not_in'] ) ) {
$args['post__not_in'] = explode( ',', $atts['not_in'] );
}
return $args;
}
view raw display-posts-exclude-posts.php hosted with ❤ by GitHub

Now should this be made into a plugin or not? In my opinion, it should. Many would argue by default this should reside in a plugin simply because now you are immediately impacting content. If a user or the client were to switch themes, then in places that use the [ display-posts] shortcode, only the shortcode appears without the modification.
To make it into a plugin, we just need to add the file header. Below you will see the file header and the plugin header comments, which WordPress reads and uses for the plugins page.

<?php
/**
* Display Posts Shortcode Extension Plugin
*
* @package DT_Display_Posts
* @author Travis Smith <[email protected]>
* @license GPL-2.0+
* @link http://wpsmith.net
* @link http://wordpress.org/plugins/display-posts-shortcode/
* @copyright 2014 Travis Smith
*
* @wordpress-plugin
* Plugin Name: Display Posts Shortcode - My Extension
* Plugin URI: http://www.wpsmith.net
* Description: Extends Display Posts Shortcode
* Version: 1.0.0
* Author: Travis Smith
* Author URI: http://www.wpsmith.net
* Text Domain: displayposts-ext
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
*/
add_filter( 'display_posts_shortcode_args', 'dpse_shortcode_exclude_posts', 10, 2 );
/**
* Add "not_in" arg to Display Posts Shortcode
*
* @since 1.0.0
*
* @param array $args Default Display Posts Shortcode args.
* @param array $atts Original shortcode attributes.
* @return array $args Modified Display Posts Shortcode args.
*/
function dpse_shortcode_exclude_posts( $args, $atts ) {
if( isset( $atts['not_in'] ) ) {
$args['post__not_in'] = explode( ',', $atts['not_in'] );
}
return $args;
}
view raw display-posts-shortcode-ext.php hosted with ❤ by GitHub

Yet, this brings up another issue. How do I deactivate my plugin if the extended plugin was deactivated? We will discuss this in the next post on Tuesday.

Written by Travis Smith · Categorized: WordPress

  • « Previous Page
  • 1
  • …
  • 5
  • 6
  • 7
  • 8
  • 9
  • …
  • 60
  • Next Page »

Need Help?

Please let us know how we can help you!

Get Help

Recommendations

Genesis WordPress Framework
Sucuri Security
Gravity Forms
GetSoliloquy
Get Envira
Scribe SEO
BackupBuddy
WordPress Video User Manuals

Recent Posts

  • Solving WordPress 5XX Server Errors on SiteGround
  • Hiding an User in the WordPress Admin
  • Custom Rewrite Rules for Custom Post Types and Taxonomies
  • WordPress JavaScript Manager Native Functions
  • Causes of WordPress Site Performance Slowdown

About Travis

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in