How to Replace ‘Enter title here’ Text in WordPress

When we create a custom post and click Add New Post,we see everything same at the admin end. For example you added a new custom post Team. So instead of “Enter Title Here” on the Title section you want to show “Name of Team Member”. How can we do that.

First you have a create a custom post as I create a custom post Team. get_current_screen() is a function that is used for get the current screen object. I create a function change_default_title() and use get_current_screen() function inside it. And finally filtering the function by use add_filter. At the first parameter ‘enter_title_here’ is the current version. And you can change it by add the function name  at the second parameter.



// Change default Title
function change_default_title( $title ){
$screen = get_current_screen();
	
// For Team
if ( 'team' == $screen->post_type ) {
$title = 'Name of Team Member';
}   

return $title;
}

add_filter( 'enter_title_here', 'change_default_title' );

When you want to change ‘Enter Title Here’ in multiple custom posts. You can use like this.


// Change default Title
function change_default_title( $title ){
    $screen = get_current_screen();

    // For Testimonials
    if  ( 'testimonials' == $screen->post_type ) {
        $title = 'Customer Name';

    // For Services
    } elseif ( 'services' == $screen->post_type ) {
        $title = 'Service Name';

    // For Projects
    } elseif ( 'projects' == $screen->post_type ) {
        $title = 'Project Name';
    }
	
	// For Slider
    elseif ( 'slider' == $screen->post_type ) {
        $title = 'Slider Heading';
    }
	
	// For Team
    elseif ( 'team' == $screen->post_type ) {
        $title = 'Name of Team Member';
    }   

    return $title;
}

add_filter( 'enter_title_here', 'change_default_title' );

Hope this tutorial works. Thanks !

Creating a custom table with PHP in WordPress

Hello Wp Rockers,
If you work with WordPress database then you must familiar
with some WordPress tables like wp_users, wp_posts, wp_comments etc.

Now if you want to create some more tables with custom columns.
Then here is a cool solution for you.

I’m going to create a new table wp_account. So I’m using
CREATE TABLE sql and set my column names with data types.
Take a look on the codes.


<?php
// CREATE TABLE WP_ACCOUNT
global $wpdb;
$table_name = $wpdb->base_prefix."account";

if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name){
$sql = "CREATE TABLE ".$wpdb->base_prefix."account(
account_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

client_id VARCHAR(200) NOT NULL,

total_payment VARCHAR(200) NOT NULL,

payment_date VARCHAR(200) NOT NULL,

work_title VARCHAR(200) NOT NULL,

details LONGTEXT NULL
)";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql);
}
?>

First I’m using $wpdb->base_prefix for use the current prefix (Example: wp_ ).
I checked the table already exist or not by using $wpdb->get_var. Finally dbDelta for manupulate the sql.

Use this code. Refresh your database and check the new table wp_account with all the custom fields.

Another thing, if you want to know about How to remove a table in WordPress, please check the tutorial.

Delete tables from Database when deactivate WordPress plugin

While you delete a WordPress plugin then you may
want to remove all your database tables that used in your plugin.

There is a hook in WordPress register_deactivation_hook() . It will
work when plugin deactivate. And we want a database table ‘people’ will drop when plugin deactivated. Here is the code for that.


<?php
// Delete table
function delete_people_table() {
global $wpdb;
$sql = "DROP TABLE IF EXISTS people";
$wpdb->query($sql);
delete_option("my_plugin_db_version");
}
register_deactivation_hook( __FILE__, 'delete_people_table' );
?>

I create a function delete_people_table(). Inside this function
I use $wpdb->query for manupulate the DROP TABLE sql. Finally use the
deactivation hook and pass ‘delete_people_table’ function into the parameter.

Hope this tutorial helps.

How to retrieve the slug of current WordPress page ?

Page slug means the name of the page. When we add a new page in WordPress then WordPress automatically create the name of the page based on the page title we provide. For example the page slug of this page is how-to-retrieve-the-slug-of-current-wordpress-page .

At the admin end there is also a page slug for every single admin pages. When we create a custom admin menu, it has a page slug like this          wp-admin/admin.php?page=sample-page-slug . 

Now important thing is how we can retrieve the page slug if we want. There is a function in WordPress get_post_field()  for this work.


$post_slug = get_post_field( 'post_name', get_post() );
echo $post_slug;

Here ‘post_name’ is a post field. This function retrieve data from the post field based on the ID of the post. Some other post fields are – ‘post_type’, ‘post_status’, ‘post_content’, etc. When we click on a custom post type. We can see the post field and slug like this

wp-admin/edit.php?post_type=services

I hope this tutorial helps you to understand the concept clearly.

Thanks !

How to Display Today’s Date in WordPress

If you are creating any Blog, Magazine or any
content based WordPress website, then you may need to
show the current date on the top of the website.

You can use php date function for this. Inside function
you can change the formate by parameter formate changes.


<?php
echo date('l jS F Y');
?>

This funciton return output in following formate.

Monday, 11th September 2017

Here is more options that you can use to chnage the date formate.

<?php
$date = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$date = date("m.d.y"); // 03.10.01
$date = date("j, n, Y"); // 10, 3, 2001
$date = date("Ymd"); // 20010310
$date = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$date = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$date = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$date = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$date = date("H:i:s"); // 17:16:18
$date = date("Y-m-d H:i:s"); // 2001-03-
?>

Thanks for reading this article.
Hope it will help you a lot.