WordPress Tip – Print parent page title and if there is no parent, print current page title.

Got this requirement on one my projects to show the Parent Page Title if there is a parent page and if there is no parent page to show the current page title. To do that use the following code,

echo empty( $post->post_parent ) ? get_the_title( $post->ID ) : get_the_title( $post->post_parent );

WordPress / PHP / Magic Fields Strip unnecessary HTML Tags while leaving the ones you want

HI Its really annoying when WordPress sometimes adds tags in multiline text inputs when you really dont need them. So to get rid of those tags you can use the following php function,


Here the string part refers to the string that your getting through WordPress or maybe as a Magic fields input field. and you can use the allow part to allow any tags that you might actually want to leave in the out put.


strip_tags($postextract, '<a><ul>');

This will actually strip all HTML tags leaving anchor tags and UL tags in the output. Pretty useful hack. 🙂

Reference :

Allow only Numbers and Decimals in a text box

To allow only Numbers and Decimals in Text boxes you can use the following JavaScript code,

<SCRIPT language=Javascript>
       function isNumberKey(evt)
          var charCode = (evt.which) ? evt.which : event.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;

And at the Text box call the function on Key Press event,

onkeypress="return isNumberKey(event)"

Add css styling to the last item of a list / Menu

To apply css styling to the last item on a list you can use the following syntax,

.ul li:last-child {
border-right: 1px solid #806862;