Comments for WordPress Developer Resources https://developer.wordpress.org Official WordPress Developer Resources Wed, 16 Jan 2019 21:47:53 +0000 hourly 1 https://wordpress.org/?v=5.2-RC1-45274 Comment on add_theme_support() by Birgit Pauli-Haack https://developer.wordpress.org/reference/functions/add_theme_support/#comment-3120 Sat, 23 Mar 2019 20:54:59 +0000 http://developer.wordpress.org/reference/functions/add_theme_support/#comment-3120 Enabling theme support for align full and align wide option for the block editor, use
add_theme_support( 'align-wide' );

]]>
Comment on add_thickbox() by fnctn https://developer.wordpress.org/reference/functions/add_thickbox/#comment-3055 Fri, 25 Jan 2019 21:54:22 +0000 http://developer.wordpress.org/reference/functions/add_thickbox/#comment-3055 If you use the following without a hook:

add_thickbox();

You will see the following PHP warning if debug mode is on:

Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information.

Notice: wp_enqueue_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information.

To prevent this issue when calling the method, use one of the three hooks mentioned in the error as so:

function fnctn_add_thickbox () {
    add_thickbox();
}
add_action ( 'wp_enqueue_scripts','fnctn_add_thickbox' );
]]>
Comment on register_sidebar() by Tazo Todua https://developer.wordpress.org/reference/functions/register_sidebar/#comment-3041 Wed, 16 Jan 2019 21:47:53 +0000 http://developer.wordpress.org/reference/functions/register_sidebar/#comment-3041 To output registered sidebar:

<?php if ( is_active_sidebar( 'your-sidebar-slug' ) ) { ?>
	<ul id="sidebar">
		<?php dynamic_sidebar('your-sidebar-slug'); ?>
	</ul>
<?php } ?>
]]>
Comment on comment_reply_link() by muze233 https://developer.wordpress.org/reference/functions/comment_reply_link/#comment-3039 Mon, 14 Jan 2019 17:29:58 +0000 http://developer.wordpress.org/reference/functions/comment_reply_link/#comment-3039 按照文档,可以这样: __('回复' , 'muze'), 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>

]]>
Comment on register_block_type() by jmau https://developer.wordpress.org/reference/functions/register_block_type/#comment-3038 Sun, 13 Jan 2019 22:47:10 +0000 https://developer.wordpress.org/reference/functions/register_block_type/#comment-3038 You can pass custom $attributes which can be used both on editor and front-end in render_callback :

register_block_type( 'my_namespace/my_block', [
	'render_callback' => 'render_callback',
	'attributes'      => [
		'some_string' => [
			'default' => 'default string',
			'type'    => 'string'
		],
		'some_array'  => [
			'type'  => 'array',
			'items' => [
				'type' => 'string',
			],
		]
	]
] );

Important (tested in 5.0.3) : in case of array attributes you MUST specify items type. Otherwise it would trigger a notice.

]]>
Comment on WP_Screen by johnzenith https://developer.wordpress.org/reference/classes/wp_screen/#comment-3037 Fri, 11 Jan 2019 17:32:44 +0000 http://developer.wordpress.org/reference/classes/wp_screen/#comment-3037 How to configure the current screen ‘layout_columns’ and ‘per_page’ options:

// Set the default screen layout columns
add_screen_option( 'layout_columns', array(
    'max'     => 2,
    'default' => 2,
) );

// Set screen per page option
add_screen_option( 'per_page', array(
    'default' => 10,
    'label'   => __( 'Items Per Page', 'uexplorer' ),
    
    /** 
     * Note:
     *
     * The 'option' argument specify what [name] to use to save the 'per_page' option
     * For example, if 'option' => 'my_per_page_option', then to retrieve the option value do: 
     *    $screen->get_option( 'per_page', 'option' )
     *
     * But when the 'option' argument is set to false or omitted, the current screen ID will be 
     * used and suffixed with '_per_page' as the current screen 'per_page' option name:
     *
     * This is how the 'per_page' option name is set when omitted or set to false:
     * $option = str_replace( '-', '_', "{$screen->id}_per_page" );
     *
     * Then to retrieve it, do: 
     * $per_page = $screen->get_option( 'per_page', str_replace( '-', '_', "{$screen->id}_per_page" ) );
     */
    'option'  => false, // use the current screen ID and append '_per_page' to it. Also replace '-' with '_'
) );
]]>
Comment on restrict_manage_posts by Roy Tanck https://developer.wordpress.org/reference/hooks/restrict_manage_posts/#comment-3034 Wed, 09 Jan 2019 11:39:30 +0000 http://developer.wordpress.org/reference/hooks/restrict_manage_posts/#comment-3034 The example below eliminates the need for parse_query when filtering based on taxonomy. It uses the taxonomy slug as URL parameter, just like WordPress does out of the box. The example adds filter dropdowns for two taxonomies, and allows for boolean AND filtering.

add_action( 'restrict_manage_posts', 'add_admin_filters', 10, 1 );

public function add_admin_filters( $post_type ){
	if( 'my_post_type' !== $post_type ){
		return;
	}
	$taxonomies_slugs = array(
		'my_taxonomy',
		'my_other_taxonomy'
	);
	// loop through the taxonomy filters array
	foreach( $taxonomies_slugs as $slug ){
		$taxonomy = get_taxonomy( $slug );
		$selected = '';
		// if the current page is already filtered, get the selected term slug
		$selected = isset( $_REQUEST[ $slug ] ) ? $_REQUEST[ $slug ] : '';
		// render a dropdown for this taxonomy's terms
		wp_dropdown_categories( array(
			'show_option_all' =>  $taxonomy->labels->all_items,
			'taxonomy'        =>  $slug,
			'name'            =>  $slug,
			'orderby'         =>  'name',
			'value_field'     =>  'slug',
			'selected'        =>  $selected,
			'hierarchical'    =>  true,
		) );
	}
}
]]>
Comment on remove_post_type_support() by arifktk https://developer.wordpress.org/reference/functions/remove_post_type_support/#comment-3009 Fri, 14 Dec 2018 06:40:09 +0000 http://developer.wordpress.org/reference/functions/remove_post_type_support/#comment-3009 Hide page visual editor if certain template is selected:

add_action( 'init', 'remove_editor_init' );

function remove_editor_init() {
    // If not in the admin, return.
    if ( ! is_admin() ) {
       return;
    }

    // Get the post ID on edit post with filter_input super global inspection.
    $current_post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
    // Get the post ID on update post with filter_input super global inspection.
    $update_post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT );

    // Check to see if the post ID is set, else return.
    if ( isset( $current_post_id ) ) {
       $post_id = absint( $current_post_id );
    } else if ( isset( $update_post_id ) ) {
       $post_id = absint( $update_post_id );
    } else {
       return;
    }

    // Don't do anything unless there is a post_id.
    if ( isset( $post_id ) ) {
       // Get the template of the current post.
       $template_file = get_post_meta( $post_id, '_wp_page_template', true );

       // Example of removing page editor for page-your-template.php template.
       if (  'page-your-template.php' === $template_file ) {
           remove_post_type_support( 'page', 'editor' );
           // Other features can also be removed in addition to the editor. See: https://codex.wordpress.org/Function_Reference/remove_post_type_support.
       }
    }
}

Credit: https://wordpress.stackexchange.com/a/91644/138483

]]>
Comment on wp_footer by 1naveengiri https://developer.wordpress.org/reference/hooks/wp_footer/#comment-3006 Thu, 13 Dec 2018 15:49:15 +0000 http://developer.wordpress.org/reference/hooks/wp_footer/#comment-3006 Example code:

function your_function() {
    echo 'This is inserted at the bottom';
}
add_action( 'wp_footer', 'your_function' );
]]>
Comment on wp_enqueue_script() by cgastrell https://developer.wordpress.org/reference/functions/wp_enqueue_script/#comment-3002 Tue, 11 Dec 2018 18:58:41 +0000 http://developer.wordpress.org/reference/functions/wp_enqueue_script/#comment-3002 Unmet dependencies will *NOT* be warned

If dependencies (3rd argument) are not met, the script tag is just not printed and no warnings nor errors are shown.

This doesn’t seem like a bug, just expected behavior. A mention about this on the docs would suffice to prevent misunderstandings.

This example will never load /js/example.js

/**
 * ... enqueue plugin scripts.
 */
function load_plugin_scripts() {
    wp_enqueue_script(
		'script-name',
		plugin_dir_url( __FILE__ ) . '/js/example.js',
		array('not-existant'),
		'1.0.0',
		true
	);
}
add_action( 'wp_enqueue_scripts', 'load_plugin_scripts' );
]]>