Create Custom Fileds in Wordpress

Without using any plugin we can create custom fields in WordPress.

In WordPress dashboard at top right corner you can see "Screen options" . Click on it and check "Custom Fields" to activate it . (But you can see "Custom Fields" option only in Post/Page Edit mode)

Custom Fields




In Page Edit Mode under content Editor you can see a Meta box "Custom Fields", In this we can see 2 fields "Name" and "value". "Name" will be your ID and Value will be the output. Create a custom field and update your post or page .

Display custom field

1. <?php the_meta(); ?> //  this displays all custom fields created in a page

place this in your header.php . I am using this as unique Slogan for every page.  

2.If you wanted to display value of one specific custom field, use the code shown below

<?php echo get_post_meta($post->ID, 'demo', true); ?> // "demo" would be ID of custom field

3. Display multiple values of same custom field ID, use this code

<?php $c_demo = get_post_meta($post->ID, 'demo', false); ?> // "demo" would be ID of custom field
<ul>
    <?php foreach($c_demo as $c_demo_display) {
        echo '<li>'.$c_demo_display.'</li>';
    } ?>
</ul>

4. Display output only if custom field exists

<?php

$logic = get_post_meta($post->ID, 'demo', true);

if($logic!='')
{
echo $logic;
}
?>







Create Shortcode in Wordpress

You can create any shortcode in wordpress by using below code format

Here i am showing you an example Shortcode for "Date".

function cdate_shortcode() {
  $shortdate = get_the_date('l, F j, Y');
  return $shortdate;
}
add_shortcode('shortdate', 'cdate_shortcode');

Place the above code in your theme's function.php file

Use [shortdate] in your Posts/pages.

Restrict content in Post or Page for Wordpress

Generally we display our content using "the_content();". But this displays entire content in Post or page.
To restrict this without using complicated code, Use the below code

$rest_content = the_content();
$content_result = substr($rest_content,'0','200') // 200 specifies the characters to display
echo $content_result;

Move your wp-content folder in Wordpress

To Make your website more secure from Hackers,  We can move wp-content folder which  contains your themes, plugins and uploads. Few plugins uses wp-content to store

their cache data.So we can make it more difficult for people to find your wp-content directory by moving it to another area of your website.

To Move wp-content folder to another location, Add this code into your wp-config.php file


define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/customfolder/wp-content' );

"Replace customfolder with your folder name (don't use special characters or Capital letters as your Folder name) "

You can add new location using the URL as shown below

define( 'WP_CONTENT_URL', 'http://www.mywebsite.com/customfolder/wp-content' );

Otherwise you to rename your wp-content folder using below code

define ('WP_CONTENT_FOLDERNAME', 'customfolder');

Moving or Renaming your wp-content folder can make WordPress website more safer, But not always because many WordPress plugin developers continue to write “wp-content” in their plugin code which may effect on plugin functionality. For such plugins we need to update manually if securing your website is your main priority!!.

Automatically Empty your Trash in WordPress

For Security purpose WordPress will keep a copy of deleted posts, pages, Comments and images in Trash until you delete them permanently. As per WordPress

Functionality Trash will clear automatically after 30 days. But this can be reduced by adding a small code into your wp-config.php file.

define ('EMPTY_TRASH_DAYS', 7); // Change 7 to number of days required

If you want make it clear, So that no unnecessary items are stored in your Trash, you can disable the trash system completely by adding this line of code to your wp-config.php file.

define ('EMPTY_TRASH_DAYS', 0);

Note:
1. Don't use Both the codes shown above at a time in wp-config.php file, however it takes top line as priority in PHP