WordPress

Filter to add new css class to li elements

Most of us are familiar with adding class from the dashboard that follows :

1. In Appearance > Menus, click the screen Option Tab

2. Under Show advanced menu properties, check CSS Classes

3. Expand any menu item to see the css class text input field

4. Enter desired class name and save menu to apply the class to the menu item

If you are searching for an option to get rid of editing each menu items to add same custom class to all in the menu items, you can go for filter

Where,

  • $classes is an array of the CSS classes that are applied to the menu item’s element,
  • $item is the current menu item,
  • $args is an object of wp_nav_menu() arguments,
  • $depth is Depth of menu item

To learn more about the filter, please can refer the codex.

Hits: 6

Standard
Programming, WordPress

Translating the month name

For translating the date in a website, we can have different solutions. But here,  I am particularly discussing about find and replace method for date translation.

Lets say we have date in format F j, Y i.e May 7, 2019 where,  Month is in English Langauge but what if you want to dispaly month in spanish or any other languages i.e. maio 7, 2019 ?

Just take care of the following steps and you can achive the translation for Month :

1. Create an array of Months where key is the default month and value is the translation for month.

2. Create function which replaces the default month with the translated month and returns the translated date string

3. Call the created funtion in step 2 and achieve the translation

 

Hits: 6

Standard
Programming, WordPress

HTML 5 Form Validation for Contact Form 7 Form Inputs

By default, CF7 form offers ajax validation for form input fields. We can instantly validate form entires by adding required attribute . For validating particular input field we can use the id of that input field and to validate all the input fields the common class ‘wpcf7-form-control’  is  useful.  With the help of  jQuery, we can add required attribute in the form input and remove the ‘novalidate’ attribute.

 

Hits: 247

Standard
Programming, WordPress

Submitting WordPress Posts from Frontend

There might be a situation where you may want to allow your visitor to submit contents or articles or news of their own. Most of you might be thinking that ‘damn i shall find a plugin for this’, well this is not as hard as you think. You dont’ need to search for plugin. Writing code is easier than finding plugin and customizing it.

Follow the following simple steps ;

    1.  Create a form with necessary fields i.e. title, content and featured image. Additional field could be : visitor first name, last name , email and category. Don’t forget to add nonce field because it protects the forms from certain types of misuse and malicious .

       
    2. Validate the form  fields

       
    3.  If the user exists, get the user id ; if the user doesn’t exist, insert the user in the database and get the id of the inserted user . Note : wp_insert_user() gives the user id of just inserted user

4. Upload the attached file inside the uploads folder and get the id of the attached file .

 

5.   Save the contents including featured image  in the dashboard .  Note : save the contents as a draft so that you can review the post and publish it later.

 

Time for writing the full code :

 

You might not believe, but you have just created the form to  allow visitors to add articles/ news/ posts . And the most important part of this form is , you can customize the form  without any hassle.

Hits: 18

Standard
Filter and Hooks, Programming, WordPress

Remove special character while uploading Media

The media having special characters in its name,  may not be visible in the front-end, therefore, we should remove the special character while  uploading media in the WordPress dashboard.   wp_handle_upload_prefilter, is admin filter that is called by the wp_handle_upload function, which helps to alter the filename before the file is moved to its destined location. The sole parameter $file, represent a single element of the $_FILES array.

Adding the aforementioned filter in the function.php, removes the special character from the name of the media prior to uploading it in WordPress dashboard.

Hits: 18

Standard