WordPress 4.7

Ján Bočínec, Webikon

@johnnypea

Developers

Post Type Templates

<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, product
*/
 
// … your code here

Theme API Goodies

get_theme_file_uri()


//{$type}_template_hierarchy
add_filter( 'category_template_hierarchy', function( array $templates ) {
    $format = get_term_meta( get_queried_object_id(), 'format', true );
    if ( $format ) {
        $new = "category-format-{$format}.php";
        $pos = array_search( 'category.php', $templates );
        array_splice( $templates, $pos, 0, $new );
    }
    return $templates;
} );

Custom Bulk Actions

//An option in the dropdown
add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions' );
 
function register_my_bulk_actions($bulk_actions) {
  $bulk_actions['email_to_eric'] = __( 'Email to Eric', 'email_to_eric');
  return $bulk_actions;
}

//Handling the form submission
add_filter( 'handle_bulk_actions-edit-post', 'my_bulk_action_handler', 10, 3 );
 
function my_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
  if ( $doaction !== 'email_to_eric' ) {
    return $redirect_to;
  }
  foreach ( $post_ids as $post_id ) {
    // Perform action for each post.
  }
  $redirect_to = add_query_arg( 'bulk_emailed_posts', count( $post_ids ), $redirect_to );
  return $redirect_to;
}

WP_Hook

  • Opravuje bugy
  • Vylepšuje logiku

Settings Registration API

  • Opravuje bugy
  • Vylepšuje logiku

(register_meta)

register_setting( 'general', 'blog_visibility', array(
    'show_in_rest' => array(
        'schema' => array(
            'enum' => array( 'private', 'public' )
        ),
    ),
    'type' => 'string',
    'default' => 'public',
))

Starter Content

 


add_action('after_setup_theme', function () {
    add_theme_support('starter-content', [
        'posts' => [
            'home',
            'about' => [
                'post_content' => file_get_contents(
                                    get_template_directory_uri() . 'content/pages/about.html'
                                    )
            ],
            'contact',
            'blog'
        ]
    ]);
});

REST API

 

https://developer.wordpress.org/rest-api/

 

  • API endpoints for WordPress data types
  • JSON
  • Not only PHP anymore!

(JavaScript Object Notation)

WordPress 4.7

By Ján Bočínec

WordPress 4.7

  • 830