✂️ WordPress

Small information nuggets and recipies about WordPress


(most recent on top)

Dequeue scripts and styles from the parent theme

// Dequeue styles & scripts from the parent theme
wp_dequeue_style( 'parenttheme-style' );
wp_dequeue_script( 'parenttheme-script' );
// Deregister handles to reuse them when enqueuing your own stuff
wp_deregister_style( 'parenttheme-style' );
wp_deregister_script( 'parenttheme-script' );

Create custom post types (in code)

Code conventions

String translation, i18n and l10n

… returns the translated string

__( 'Phrase to translate', 'text-domain' );
_x( 'Phrase to translate', 'context comment for translators', 'text-domain' );
_n( '1 singular', '%s plural', $the-number, 'text-domain' );
_nx( '1 singular', '%s plural', $the-number, 'context comment for translators', 'text-domain' );

… echoes the translated string

_e( 'Phrase to translate', 'text-domain' );
_ex( 'Phrase to translate', 'comment context for translators', 'text-domain' );

Manually activate as user

  1. Get the activation key for the user from table wp-signup
  2. Go to url: /wp-activate.php?key=...

Wordpress installation url

… HF: Always use this first one

bloginfo(‘wpurl’)       // same as setting "WordPress address (URL)"
bloginfo(‘url’)         // same as setting "Site address (URL)"

wp-config settings

Auth keys

Override domain (dev)

define( 'WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME'] );
define( 'WP_HOME',    'http://' . $_SERVER['SERVER_NAME'] );

Debug settings (dev)

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );         # error_log( "debug message" );
define( 'WP_DEBUG_DISPLAY', false );
define( 'SCRIPT_DEBUG', true );
define( 'SAVEQUERIES', true );

Plugins

Global paths

if ( !defined( 'MYPLUGIN_XXXXX' ))
    define( 'MYPLUGIN_MYPLUGIN_XXXXX', ABSPATH . _____ );

HF: Best way to retrieve THEME_DIR, PLUGIN_NAME, PLUGIN_DIR, PLUGIN_URL?

Define version

if ( !defined( 'MYPLUGIN_VERSION_KEY' )) define( 'MYPLUGIN_VERSION_KEY', 'myplugin_version' );
if ( !defined( 'MYPLUGIN_VERSION_NUM' )) define( 'MYPLUGIN_VERSION_NUM', '1.0.0' );
add_option( MYPLUGIN_VERSION_KEY, MYPLUGIN_VERSION );

HF: Does this need to be duplicated somewhere in the plugin “metadata”?

Version upgrades

$new_version = '2.0.0';
if ( get_option( MYPLUGIN_VERSION_KEY ) != $new_version ) {
    // Execute your upgrade logic here
    update_option( MYPLUGIN_VERSION_KEY, $new_version );
}