WordPress Useful tips

29 Oct 2020

Here are the most useful WordPress tips and hacks to help you use WordPress

Here are the most useful WordPress tips and hacks to help you use WordPress

Here are the most useful WordPress tips and hacks to help you use WordPress

Here are the most useful WordPress tips and hacks to help you use WordPress

Here are the most useful WordPress tips and hacks to help you use WordPress

activate

register_activation_hook( __FILE__, 'pluginprefix_function_to_run' );
register_deactivation_hook( __FILE__, 'pluginprefix_function_to_run' );
flush_rewrite_rules(); Clear the permalinks after the post type has been registered.
register_uninstall_hook(__FILE__, 'pluginprefix_function_to_run'); OR uninstall.php
//if uninstall.php is not called by WordPress, die
if (!defined('WP_UNINSTALL_PLUGIN')) {
    die;
}

option

$option_name = 'wporg_option';
delete_option($option_name);
// for site options in Multisite
delete_site_option($option_name);

wpdb

// drop a custom database table
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}mytable");

check before init

// Check for Existing Implementations #
if ( !function_exists( 'wporg_init' ) )
if ( !class_exists( 'WPOrg_Plugin' ) ) {
if ( is_admin() ) {}

Determining Plugin and Content Directories

plugin

plugins_url()
plugin_dir_url()
plugin_dir_path()
plugin_basename()

theme

get_template_directory_uri()
get_stylesheet_directory_uri()
get_stylesheet_uri()
get_theme_root_uri()
get_theme_root()
get_theme_roots()
get_stylesheet_directory()
get_template_directory()

site home

home_url()
get_home_path()

wordpress

admin_url()
site_url()
content_url()
includes_url()
wp_upload_dir()

constants

WP_CONTENT_DIR  // no trailing slash, full paths only
WP_CONTENT_URL  // full url
WP_PLUGIN_DIR  // full path, no trailing slash
WP_PLUGIN_URL  // full url, no trailing slash
UPLOADS

roles

By default, WordPress have six roles:

  • Super Admin // multisite network
  • Administrator
  • Editor
  • Author
  • Contributor
  • Subscriber
add_role()
remove_role( 'simple_role' );
update_option('default_role', YOUR_NEW_DEFAULT_ROLE)
$role = get_role( 'simple_role' );
// Add a new capability.
$role->add_cap( 'edit_others_posts', true );
user_can( $user, $capability );
current_user_can( $capability );

security

built-in php

isset() and empty() for checking whether a variable exists and isn’t blank
mb_strlen() or strlen() for checking that a string has the expected number of characters
preg_match(), strpos() for checking for occurrences of certain strings in other strings
count() for checking how many items are in an array
in_array() for checking whether something exists in an array

wp

is_email() will validate whether an email address is valid.
term_exists() checks whether a tag, category, or other taxonomy term exists.
username_exists() checks if username exists.
validate_file() will validate that an entered file path is a real path (but not whether the file exists).

input => sanitize_*()
output => esc_*

localization

_e() or __().
can do esc_html_e( 'Hello World', 'text_domain' );

skip action

// If save_post has been run more than once, skip the rest of the code.
if ( did_action( 'save_post' ) !== 1 ) {
return;
}
add_menu_page(
string $page_title,
string $menu_title,
string $capability,
string $menu_slug,
callable $function = '',
string $icon_url = '',
int $position = null
);

submenu

add_submenu_page(
string $parent_slug,
string $page_title,
string $menu_title,
string $capability,
string $menu_slug,
callable $function = ''
);

add_*_page // to add to worpdress menu pages

add_action( 'admin_menu', 'wporg_options_page' );
<form action="<?php menu_page_url( 'wporg' ) ?>" method="post"/>

submit hook

add_action( 'admin_menu', 'wporg_options_page' );
function wporg_options_page() {
$hookname = add_menu_page(
'WPOrg',
'WPOrg Options',
'manage_options',
'wporg',
'wporg_options_page_html',
plugin_dir_url(__FILE__) . 'images/icon_wporg.png',
20
);

add_action( 'load-' . $hookname, 'wporg_options_page_submit' );
}

transient

set_transient( $transient, $value, $expiration );
get_transient( $transient );
delete_transient( $transient );

shortcode

function wporg_shortcode( $atts = array(), $content = null ) {
// do something to $content

// always return
return $content;
}
add_shortcode( 'wporg', 'wporg_shortcode' );

ajax check

check_ajax_referer( 'wpdocs-special-string', 'security' );

Delete posts

delete a,b,c,d
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id )
WHERE e.term_id = X