chihung
11/14/2018 - 1:28 AM

wordpress-snippets.md

WordPress snippets

Hide "Strict Standards" warning message PHP5.4 over

.php

ini_set('error_reporting', E_ALL | ~E_STRICT);

.htaccess

# <? ?> is allowed
php_flag short_open_tag On

Add page template name

/**
 * Template Name: Name
 */

Get link

<a href="<?php echo esc_url(home_url('xxx/')); ?>">

Path

# image path
<img src="<?php echo esc_url(get_template_directory_uri()); ?>/img/common/header/logo.png">

# /var/www/html/example/wp-content/themes/twentyten
get_template_directory()

# include template
get_template_part('container', 'xxx');

isDevelopment

$isDevelopment = strpos( $_SERVER["HTTP_HOST"], "localhost" ) !== false;

Show new label only while 1 month

<?php

if ( date( 'U' ) - get_the_time( 'U', $post->ID ) < 24 * 60 * 60 * 30 ):
?>
    <span class="label label-red">NEW</span>
<?php
endif;
?>

Template Hierarchy

https://developer.wordpress.org/themes/basics/template-hierarchy/

Visual Overview

https://developer.wordpress.org/files/2014/10/wp-hierarchy.png

Access all post data

https://codex.wordpress.org/Template_Tags/get_posts#Access_all_post_data

<?php

global $post;

$lastposts = get_posts( array(
    'posts_per_page' => 3,
    'post_type'      => 'news',
) );

foreach ( $lastposts as $post ) :
    setup_postdata( $post ); ?>
    <h2>
        <a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
        </a>
    </h2>
    
    <?php the_content(); ?>
<?php
endforeach;
wp_reset_postdata();
?>

Category + post count on sidebar

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">
            CATEGORIES
        </h3>
    </div>
    <div class="list-group">
        <?php
        $categories = get_categories();

        foreach ( $categories as $category ) {
            ?>
            <a href="<?php echo get_category_link( $category->term_id ); ?>" class="list-group-item">
                <?php
                echo $category->name;
                ?>
                <span class="badge">
                    <?php
                    echo $category->category_count;
                    ?>
                </span>
            </a>
            <?php
        }

        ?>
    </div>
</div>
/*
 *
---------------------------------------------------------*/
function my_excerpt_length( $length ) {
    return 80;
}

add_filter( 'excerpt_length', 'my_excerpt_length' );

jQuery in WordPress

jQuery(document).ready(function( $ ) {
  $('.element').on('click', function() {
    $(this).hide();
  })
});

Contact form 7

[contact-form-7 id="1234" title="Contact form 1" html_id="contact-form-1234" html_class="container-form"]

Display biz calendar widget

http://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/the_widget

<?php the_widget( 'bizcalendarwidget' ); ?>

Get title

echo get_the_title( get_option('page_for_posts', true) );
post_type_archive_title();

Get category

// Category
$cat = get_the_category();
$catId = $cat[0]->cat_ID;
$catName = $cat[0]->name;
$catSlug = $cat[0]->category_nicename;
$link = get_category_link($catId);

// Custom post category(taxonomy)
$category = get_the_terms( $post->ID, 'product-category' );
$catName = $category[0]->name;

Stop adding auto p or br

https://setting-tool.net/wordpress-p-br-auto-format

Post_thumbnail

<?php
if ( '' !== get_the_post_thumbnail() ) :
    ?>
    <p>
        <?php
        the_post_thumbnail( 'post-thumbnail', array(
            'class' => 'img-responsive'
        ) );
        ?>
    </p>
<?php
endif;
?>

Truncate

// title
echo mb_strimwidth(get_the_title(), 0, 60, "...", "UTF-8");

// content
$content = wp_strip_all_tags( get_the_content() );
echo nl2br(mb_strimwidth($content, 0, 100, "...", "UTF-8"));

header & footer

<?php

get_header();// get header.php
get_footer();// get footer.php

wp_head();
wp_footer();

body class

<body <?php body_class(); ?>>

Active status implementation

$post_type = get_query_var( 'post_type' );// if custom post type.
var_dump($post_type);

<ul class="nav navbar-nav">
    <li<?php echo (is_home()) ? ' class="active"' : ''; ?>>
        <a href="<?php echo esc_url(home_url('/')); ?>">
            <img src="<?php echo esc_url(get_template_directory_uri()); ?>/img/nav/top.png" alt="トップページ TOP">
        </a>
    </li>
    <li<?php echo ($post_type == 'products') ? ' class="active"' : ''; ?>>
        <a href="<?php echo esc_url(home_url('products/')); ?>">

Short code in theme

echo do_shortcode('[addtoany]');

Useful plugin

Custom Field Suite


https://ja.wordpress.org/plugins/custom-field-suite/ (can connect Page template name)

Unknown collation: 'utf8mb4_unicode_520_ci'

http://qiita.com/wonda/items/ee33ec26028e52cf9336

Can use WordPress resources on outside WordPress directory.

require_once('../wp-blog-header.php');

WordPress get variaus path or url.

http://oxynotes.com/?p=8590

Can add "/" slash to editable permalink by Custom Permalinks plugin.

http://qiita.com/OJI3T/items/f3cd1219cb722b0c7eb8

functions.php

https://gist.github.com/regepan/6d9767fd56339d4bdfe501aad14cfde3

Custom Post Type UIの一覧記事取得

Change password via md5 hash.

https://codex.wordpress.org/Resetting_Your_Password#Through_MySQL.2FMariaDB_Command_Line

Database Search and Replace Script in PHP

https://interconnectit.com/products/search-and-replace-for-wordpress-databases/ https://interconnectit.com/blog/2009/10/07/migrating-a-wordpresswpmubuddypress-website/

add_rewrite_rule()

https://nskw-style.com/2014/wordpress/wordpress-app-with-rewrite-api.html