Wordpress

migrate wordpress

sqls:

select * from wp_options where option_name in ('siteurl', 'home');
update wp_options set option_value = 'http://xxx.xxx' where option_name in ('siteurl', 'home');

wp-cli

install Wordpress core

$ mkdir wp
$ cd wp
$ wp core download
$ wp core config --dbname=wpdemo --dbuser=wpdemo --dbpass=wpdemo
$ wp core install --url=http://wpdemo.local/ --title=wpdemo --admin_user=admin --admin_password=admin --admin_email=admin@example.com

install and activate plugins

wp plugin install plugin-name-here --activate

plugins to consider:

custom-post-type-ui
advanced-custom-fields
shortcodes-ultimate
post-duplicator

contact-form-7
contact-form-7-honeypot

theme-my-login

popup-maker

# for customizing Woothemes child theme
https://github.com/woothemes/theme-customisations/archive/master.zip

search and replace

testing:

wp search-replace --dry-run 'dev.example.com' 'www.example.com'

actually run:

wp search-replace 'dev.example.com' 'www.example.com'

wordpress multilanguage

refer: 4 WAYS TO TURN WORDPRESS INTO A MULTILINGUAL WEBSITE

one post per language

WPML (Premium)

pros:

cons:

alternative: Polylang, xili-language, Bogo

all languages in one post

qTranslate (free)

pros:

cons:

alternative: qTranslate-X, WPGlobus

automatic translation

multisite solution

Multilingual Press

pros:

cons:

alternative: Multisite Language Switcher, Zanto

Child theme

always create a child theme when you want to extend a existing theme

create a folder in the themes folder, create two files:

reference parent theme in style.css

/*
Theme Name: child-thme-name
Template: parent-theme-name
*/

parent theme's style.css will be overridden, see below to include

create custom functions in functions.php

<?php
function theme_enqueue_styles() {
    wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');

    // add styles and scripts here
    wp_register_style('bootstrap', get_stylesheet_directory_uri() . '/css/bootstrap.min.css', '', '0.3.2');
    wp_enqueue_style('bootstrap');

    wp_register_script('bootstrap', get_stylesheet_directory_uri() . '/js/bootstrap.min.js', '', '0.3.2' );
    wp_enqueue_script('bootstrap');
}

add_action('wp_enqueue_scripts', 'theme_enqueue_styles');

register_nav_menus( array(
    'footer_nav' => __( 'Footer Nav', 'twentysixteen' ),
) );

?>

notes:

quick sql

menu items

SELECT * FROM wp_posts WHERE post_type='nav_menu_item';
SELECT * FROM  `wp_postmeta` WHERE meta_key LIKE  '%_menu_item_%';

check and update links (check any absolute links before go live/moving domain)

check links do not go to devleopment site before put the site alive:

SELECT * FROM `wp_options` where option_value like '%isindevelopment%';
SELECT * 
    FROM  `wp_posts` 
    WHERE post_status =  'publish'
    AND post_content LIKE  '%isindevelopment%'
    LIMIT 0 , 30;
SELECT * FROM `wp_postmeta` where meta_value like '%isindevelopment%';

UPDATE `wp_postmeta` set meta_value = REPLACE(meta_value, 'XXXXXXX', 'YYYYYYYYYYY') WHERE meta_key = '_menu_item_url' AND meta_value LIKE '%isindevelopment%';

Woocommerce

Debug

you can enable debug mode in wp-config.php by setting WP_DEBUG to TRUE, using WP_DEBUG_DISPLAY and WP_DEBUG_LOG to make sure that your error messages aren`t shown to all the visitors but rather saved to a log file.

WP loading process

index.php -> wp-blog-header.php 
                         |-> wp-load.php -> wp-config.php -> wp-settings.php
                         |-> wp()
                         |-> template-loader.php

important hooks in wp-settings.php

export / import

if you want to import media library from another wp install, do this

  1. download the upload folder and merge to the new site;
  2. export the attachment post type from the old site's posts and postmeta tables and import to new site

refer: Importing WordPress attachments into Media Library

steps:

export data for the following two sqls

SELECT * FROM wp_posts WHERE post_type = 'attachment' AND post_parent != '0';
SELECT * FROM wp_postmeta WHERE post_id IN ( SELECT ID FROM wp_posts WHERE post_type = 'attachment' AND post_parent != '0' );

In the wp_posts.sql file I did the following:

First I change all the INSERT INTO sentences to INSERT IGNORE INTO in case duplicate keys exists It don`t break them
I modified all image urls to match new domain (not always needed, depending where you are importing from)
I removed all unnecessary SQL code such as ALTER TABLE and INSERT TABLE and just left the INSERT INTO SENTENCES
Zipped the file and imported into new site db with phpmyadmin

In the wp_postmeta.sql file I did the following:

I removed all the unnecessary SQL code such as ALTER TABLE and INSERT TABLE and just left the INSERT INTO SENTENCES
I changed all the INSERT INTO `wp_postmeta` ( `meta_id`, `post_id`, `meta_key`, `meta_value`) VALUES to INSERT INTO `wp_postmeta` ( `post_id`, `meta_key`, `meta_value`) VALUES . Basically Im removing meta_id because I want to insert all the postmeta at the end of the table and use the auto increment
I did a regex search and replace to remove all ID from values. I used \( ([0-9]+), and replaced with just (
Zipped file and imported to new site

Theme tips

display content of multiple pages on one page:

SCRN 
http://sugartree.co.nz/

Ajax

Smashing magazine Wordpress Ajax process

query object

ref

build and use a custom query object:

wp_reset_postdata(); // reset the post meta

$query = new WP_Query(array(
        'post_type' => 'page',
        'page_category' => 'apple',
        'posts_per_page' => -1,
        ));

if ( $query->have_posts() ) while ( $query->have_posts() ) : $query->the_post();

the_title();

Customizer:

Wordpress Customizer

include a customizer.php in your theme's functions.php

a basic example (add a phone number in the header):

function gary_customize_register( $wp_customize ) {
    // the class should be included inside the hook, WP_Customize_Control only loads in Customizer interface
    class gary_Customize_Textarea_Control extends WP_Customize_Control {
        public $type = 'textarea';

        public function render_content() {
            echo '<label>';
            echo '<span class="customize-control-title">' . esc_html( $this-> label ) . '</span>';
            echo '<textarea rows="2" style ="width: 100%;"';
            $this->link();
            echo '>' . esc_textarea( $this->value() ) . '</textarea>';
            echo '</label>';
        }
    }

    $wp_customize->add_section( 'gary_contact' , array(
            'title' => __( 'Contact Details', 'gary')
    ) );

    $wp_customize->add_setting( 'gary_telephone_setting', array (
            'default' => __( 'Your telephone number', 'gary' )
    ) );
    $wp_customize->add_control( new gary_Customize_Textarea_Control(
            $wp_customize,
            'gary_telephone_setting',
            array(
                    'label' => __( 'Telephone Number', 'gary' ),
                    'section' => 'gary_contact',
                    'settings' => 'gary_telephone_setting'
            )));
}

add_action( 'customize_register', 'gary_customize_register' );

// add a hook to display the info
function gary_display_contact_details_in_header() { ?>
    <div class="phone-number">
        <?php echo get_theme_mod( 'gary_telephone_setting', '0800 GARY' ); ?>
    </div>
<?php }
add_action( 'gary_header', 'gary_display_contact_details_in_header' );

image sizes

Post Thumbnails
Responsive Images in WordPress 4.4
Using Responsive Images (Now)
getthepost_thumbnail

by default, Wordpress got thumbnail, medium, large and full image sizes, a medium_large size is added in Wordpress 4.4 (which is 768px wide by default)

When a theme adds post-thumbnail support, a special post-thumbnail image size is registered, which differs from the default thumbnail image size, the Feature Image meta box will only be available after this is added

template hierarchy

The WordPress Template Hierarchy

wordpress template hirerarchy map

Queries and Loops

pretty good article on wpmudev: WordPress Development for Intermediate Users: Queries and Loops Codex - Query Overview

get_posts example

/* arguments */
$args = array(
    'sort_order' => 'desc',
    'sort_column' => 'date',
    'number' => '5',
);

// now run get_posts and check that any are returned
$myposts = get_posts( $args );
if  ( $myposts ) { ?>

    <h2>Latest Posts</h2>

    <?php // output the posts
    foreach( $myposts as $mypost ) {

        $postID = $mypost->ID; ?>

        <article class="post recent <?php echo $postID; ?>">

            <h3><a href="<?php echo get_page_link( $postID ); ?>"><?php echo get_the_title( $postID ); ?></a></h3>
            <section class="entry">
                <?php echo get_the_excerpt( $postID ); ?>
                <a href="<?php echo get_page_link( $postID ); ?>">Read More</a>
            </section>
        </article>

<?php }

WP_Query example

Note: always use rewind_posts() after using WP_Query, so Wordpress can reset back to the main query

// arguments for query
$args = array(
    'post_type' => 'project',
    'posts_per_page' => 1
);

// run the query
$query = new WP_query ( $args );

// check the query returns posts
if ( $query->have_posts() ) { ?>

    <section class="projects">

        <?php while ( $query->have_posts() ) : $query->the_post(); ?>

        <?php //contents of loop ?>
        <h3>Latest Project - <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'medium' ); ?></a>
        <?php the_excerpt(); ?>

        <?php endwhile; ?>

    <?php rewind_posts(); ?>

        </section>

<?php } ?>

code snippets

limit 'prev', 'next' to posts in same category

javascript

default JS files and libraries in Wordpress, see tables in following pages:

wp_register_script

wp_enqueue_script

internationalization and localization

.pot, .po, .mo

refer: Wiki gettext

Tips

widget

enable shortcode in text widget

// Enable shortcodes in text widgets
add_filter('widget_text','do_shortcode');

hidden all settings page

this page http://xx.xx/wp-admin/options.php will display all setting options

Reference

Wordpress Caching Wordpress Customizer