WordPress Trips and Tricks – Part 1

How to show archive title instead of first post of custom post type

When you create a template of your custom post type for the archive data, you might need to display archive title. But by default first post title comes up. To solve this type of problem WordPress has a function named post_type_archive_title() . You just need to pass blank for the first parameter. If you need a prefix you can also pass that prefix in the first parameter.

post_type_archive_title( '', false );

How to show blog listing page title

If you want to show the page title for that page, what is set a blog post page. To do this job you need to filter title. Below is the code example.

apply_filters( 'the_title', get_the_title( get_option( 'page_for_posts' ) ) );

How to get page ID that set as a blog post

If you want to get page ID that is set as a blog post, you need to use get_option() function.

$blog_page_id = get_option( 'page_for_posts' );

What is the default taxonomy name for post category

category is the default taxonomy name for post.

Template redirection for custom post type archive or any specific category

WordPress has a default template hierarchy for any archive or any specific taxonomy of any post type. But if you want to filter it with your own path then template_include is the right filter for you to do this job.

apply_filters( 'template_include', string $template )

It basically filters the path of the current template before including it.

You can set your custom routing with the help of this filter.

Here I set a custom template for a taxonomy of a custom post type.

add_filter( 'template_include', 'your_custom_post_type_template' );
function your_custom_post_type_template( $template ) {
	$queried_object = get_queried_object();
	global $post;

	if ( $post && property_exists( $post, 'post_type' ) ) :
		if ( is_archive() && 'your-post-type' === $post->post_type ) :
			if ( property_exists( $queried_object, 'slug' ) ) :
				$template = get_template_part( 'content', 'your-template-name' );
				return $template;
			endif;
		endif;
	endif;
	return $template;
}

How to add a date into a google calendar

If you have an event website and you want to let your user to add the event date to the user google calendar account, you can directly set the link to the anchor element like below.

<a href="http://www.google.com/calendar/render?
action=TEMPLATE
&text=<?php echo esc_attr( $your_title ); ?>
&dates=<?php echo esc_attr( $event_start_date . '/' . $event_end_date ); ?>
&details=<?php echo esc_attr( $event_description ); ?>
&location=<?php echo esc_attr( $event_location ); ?>
&trp=false
&sprop=
&sprop=name:"
target="_blank" rel="nofollow">
Add to my calendar +
</a>

How to create a custom action and pass parameter when it called

WordPress has a function named add_filter() . With the help of this function you can create your custom filter with your custom functionality. You can also pass arguments when you call your filter.

Here I create a custom filter for my own what accept a parameter.

add_action( 'my-custom-filter', 'function_for_custom_filter', 10, 1 );

When you call your filter you need to pass parameter within you filter like below.

echo esc_html( do_action( 'my-custom-filter', $parameter_for_function ) );

How to verify WP nonce and return error

Verify nonce is the regular process of WordPress development. When it doesn’t match, you need to send error for the user. wp_verify_nonce() is the default function from WordPress to do this job.

if ( ! wp_verify_nonce( 'nonce_to_verify', 'your_defined_nonce' ) ) 
{
	wp_send_json_error( 'No dirty business please', 400 );
	return false;
	die();
}

You can get the your nonce with the post request.

$_POST['name_of_nonce_field']

You can get some idea how to use nonce from this link.

How to get all options of your site.

From dashboard of your WordPress site, you need to set your URL like below. It will give you all option data of your site.

https://yoursiteurl.com/wp-admin/options.php

Leave a comment

Your email address will not be published.