Twit

How to disable date filter in posts or custom post types
How to disable date filter in posts or custom post types

Sometimes there are cases when, in custom post types (created via the `register_post_type()` function), or even in default post types like post or page, we need to disable (remove) the date filter dropdown. This dropdown shows the month and year when posts were created on our site.

Searching the official documentation didn't yield much, so I had to dig into the code and find the necessary filter. I discovered the `disable_months_dropdown` filter, which takes two parameters — the display state of the dropdown and the post type.

Example of using the filter:

add_filter('disable_months_dropdown', function($bool, $post_type) {
	if($post_type == 'type_order')
	{
		return true;
	}
	return $bool;
}, 1, 2);

where:
$bool — whether to display the date dropdown (default is false, meaning the filter is disabled and the dropdown will be shown)
$post_type — the current post type

Note that you can also get the current post type using the `get_current_screen()` function and its `post_type` property. That is:

get_current_screen() -> post_type

And I have a feeling that a similar method could be used to disable the category filter as well (needs to be tested).

How to Add Custom JavaScript Code to WordPress
How to Add Custom JavaScript Code to WordPress

While working on one of the WP projects, I needed to add some JS code. Normally, for such tasks, I use separate script files. However, in this case, the code was minimal, so I decided to place it directly into the HTML "body" of the document.
To do this, use the following PHP code:

add_action('wp_enqueue_scripts', function(){
	if(!wp_script_is( 'jquery', 'done' ))
	{
		wp_enqueue_script( 'jquery' );
	}
	wp_add_inline_script( 'jquery-migrate', 'alert("Hello")' );
});

Here's what we are doing:

read more...

How to remove array element by value
How to remove array element by value

It seems like a fairly trivial task, and such a function should be built into PHP’s core — but unfortunately, it’s not. So we have to find our own solutions for implementing this functionality. One such solution for removing an array element by its value is shown below:

if(($delete_key = array_search($search_value, $my_array)) !== false)
{
	unset($my_array[$delete_key]);
}

You could say it’s just two lines — and the functionality is ready.

read more...