Skip to content

Instantly share code, notes, and snippets.

@jpgninja
Last active November 8, 2022 19:42
Show Gist options
  • Save jpgninja/cfd4b515b72384e11a562ebee0197cfc to your computer and use it in GitHub Desktop.
Save jpgninja/cfd4b515b72384e11a562ebee0197cfc to your computer and use it in GitHub Desktop.

A Collection of WordPress snippets.

Table of Contents

  1. Absolutely custom nav walker
  2. Add custom post types to main loop
  1. New wordpress install, plugin scaffolding
  2. Add new user
  3. Update domain in database
  4. Enable logging
  5. Translate small snippet of hard-coded text
  6. New user via PHP

Frontend helpers

Absolutely custom nav walker

Occasionally, you need a nav menu that outputs a menu you have absolute control over. Here's a top-to-bottom custom nav walker.

Add custom post types to main loop

Here's a quick and dirty hook that checks if we're on the main blog archive, and if we're in the main query, then looks for post types NOT built into WordPress, and adds them to the post_type, then passes the query along.

/**
 * Inject all Custom Post Types to front page main query.
 * 
 * @param $query WordPress query.
 * @return $query WordPress query.
 */
function add_CPT_to_query( $query ) {
	// Ensure we're on the front blog archive, and the main query.
	if ( is_front_page() && $query->is_main_query() ) {
		
		// Don't return built-in post types.
		$args = array(
			'_builtin' => false
		);

		// Get custom post types.
		$post_types = get_post_types( $args, 'names' );

		// Add in regular blog posts, assuming we still want these.
		$post_types['post'] = 'post';

		// Inject our custom post types to the query.
		$query->set( 'post_type', $post_types );
	}

	return $query;
}
add_action( 'pre_get_posts', 'add_CPT_to_query' );

Backend helpers

New wordpress install, plugin scaffolding

  • Base: wp plugin install backwpup gravityforms really-simple-ssl simple-history google-captcha wp-rocket autodescription atomic-blocks gutenberg

Add new user

  • SQL:
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `display_name`, `user_email`, `user_status`)
VALUES ('cm_admin', MD5('______INSERT_YOUR_PASSWORD_HERE______'), 'Chris Mewhort', 'clientcoffee', 'chrismewhort@gmail.com', '0');

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) 
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) 
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');
  • WP-Cli: wp user add (TODO)

Update domain in database

  • SQL:
/* HTTP */
UPDATE wp_options SET option_value = replace(option_value, 'http://OLDDOMAIN.com', 'http://NEWDOMAIN.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://OLDDOMAIN.com','http://NEWDOMAIN.com');
UPDATE wp_posts SET post_content = replace(post_content, 'http://OLDDOMAIN.com', 'http://NEWDOMAIN.com');

/* HTTP + WWW */
UPDATE wp_options SET option_value = replace(option_value, 'http://www.OLDDOMAIN.com', 'http://NEWDOMAIN.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.OLDDOMAIN.com','http://NEWDOMAIN.com');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.OLDDOMAIN.com', 'http://NEWDOMAIN.com');

/* HTTPS */
UPDATE wp_options SET option_value = replace(option_value, 'https://OLDDOMAIN.com', 'http://NEWDOMAIN.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'https://OLDDOMAIN.com','http://NEWDOMAIN.com');
UPDATE wp_posts SET post_content = replace(post_content, 'https://OLDDOMAIN.com', 'http://NEWDOMAIN.com');

/* HTTPS + WWW */
UPDATE wp_options SET option_value = replace(option_value, 'https://www.OLDDOMAIN.com', 'http://NEWDOMAIN.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'https://www.OLDDOMAIN.com','http://NEWDOMAIN.com');
UPDATE wp_posts SET post_content = replace(post_content, 'https://www.OLDDOMAIN.com', 'http://NEWDOMAIN.com');
  • WP-Cli: wp db searchreplace olddomain.com newdomain.com (TODO)

Enable logging

This is from the WordPress Codex. Save to wp-config.php.

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );
@ini_set( 'display_errors', true );

Quick explainer:

  • define( 'WP_DEBUG', true ); Enables WP_DEBUG mode
  • define( 'WP_DEBUG_LOG', true ); Logs to /wp-content/debug.log
  • define( 'WP_DEBUG_DISPLAY', true ); Enables display of errors and warnings
  • @ini_set( 'display_errors', true ); Attempts to force PHP to disable display of errors and warnings

Translate small snippet of hard-coded text

Translate small snippet of otherwise hard-coded text in wordpress (string must be i18n ready). This is a funky snippet that acts as a work around for a plugin or theme which has shitty code. Next time hire a real agency.

/**
 * Change hard-coded text strings.
 * 
 * @link https://www.speakinginbytes.com/2013/10/gettext-filter-wordpress/
 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
 */
function cc_custom_text_strings( $translated_text, $text, $domain ) {
  switch ( $translated_text ) {
    case 'Your Rate' :
      $translated_text = __( 'Your Rating', 'coastal' );
      break;
  }
  return $translated_text;
}
add_filter( 'gettext', 'cc_custom_text_strings', 20, 3 );

New user via PHP

Add ?cu=1 to URL after adding this to theme's functions.php:

if ($_GET['cu'] == 1) {
    $user_array = [
        'user_login' => 'cc_admin',
        'user_pass' => '____YOUR____PASSWORD____HERE____',
        'user_email' => 'chris@clientcoffee.com',
        'first_name' => 'Chris',
        'last_name' => 'Mewhort',
        'display_name' => 'Chris',
        'role' => 'administrator'
    ];
    if ( wp_insert_user( $user_array ) ) {
        echo "User created!";
        die;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment