Showing posts with label WP Hooks. Show all posts
Showing posts with label WP Hooks. Show all posts

Friday, 20 October 2017

Create admin through using FTP: WordPress

There may be some scenarios when you have to implement the changes in website content for WP where client do not have administrator credentials with him which he can share or he has forget about it or there would could be other reasons he don't have the credentials,

If you have cPanel details you can manage the things from there, and if client only shares FTP details, in that case you can follow the below steps to create a new Administrator user:
  1. Connect through valid FTP details:
  2. Visit the website and through view source code confirm the Active theme in your WP.
  3. Through FTP go to your active theme functions.php file and at the very end use the following code with proper admin details you would like to have.

  4.   function wpb_admin_account(){
       $user = 'Username';
       $pass = 'Password';
       $email = 'email@domain.com';
       if ( !username_exists( $user )  && !email_exists( $email ) ) {
       $user_id = wp_create_user( $user, $pass, $email );
       $user = new WP_User( $user_id );
       $user->set_role( 'administrator' );
       } 
      }
      add_action('init','wpb_admin_account');
    
    


  5. And do not forget to use unique username and email (as with same details you can't have other WP users).
  6. After user has been created do not forget to use delete this code from your functions.php file.
  7. And you are done!

Below is the video demonstration for above mentioned points.



Version at the time of demonstration
WP: 4.8.2


Read more

Saturday, 23 September 2017

Shortcode is not working in Sidebar Widget: WordPress

If shortcode is not working in WordPress Text Widget in that case you need to use a Hook provide by WP to enable shortcode feature for sidebar widgets.

Also would like to add that by default shortcode do not work in the sidebar Widget, they only work in post, pages and custom post type; to make shortcode work in the sidebar widget you can follow the below steps:
1) Open your themes functions.php file and add below code.

add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );


These two line should be in the same order, the first line prevent shortcode line break to convert into paragraph and second line helps to expand the shortcode in sidebar widget.

2) Add a text widget and add shortcode into it and save the widget and you are done.




Version at the time of demonstration:
WordPress 4.8.2

Read more

Wednesday, 13 September 2017

Add content/script files using WordPress wp_footer hook

There may we some cases were you can't make changes in theme footer file due to its structure, in those cases one footer hook comes handy to add html code/content/script files in your footer which is wp_footer

Below is the way we can us this hook/action:

/**
 * add custom message to footer 
 */

function myFxn() {
   echo 'string';
}

add_action('wp_footer', 'myFxn', 1);





Reference link:
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_footer



Read more

Monday, 17 July 2017

How to load Parent Style in Child Theme?

I have read about some articles regarding, how to call parent style sheet in your child theme, some prefer to load it in style using CSS import (Divi theme developers has choose this method) some has suggested to use the "wp_enqueue_scripts" hook (This hook use for both style and script files).

As you can see in this link in the Office forum of Divi Theme they suggest to use option of  to call parent theme style in child theme stylesheet.

And on Smashing Magazine article its mentioned that this is old concept of using the parent theme style in stylesheet, new approach is to load the stylesheet using the WP action wp_enqueue_scripts  in child theme functions.php file as mentioned in the below:

add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
function enqueue_parent_styles() {
   wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}

I also would like to add that; Yes, the content mentioned in the both posts has too much time difference but wp_enqueue_scripts is way back old, I mean older than 2012. Also I have seen in some websites developed by other they prefer to use the old method, some use plugins to generate child theme which goes with same way.

In the latest post author has mentioned that the older approach is not recommended the reason is performance issue.

I will now leave up to you to decide which method you would like to go with.
Read more

Tuesday, 11 July 2017

How to add excerpt to page in WordPress?

By Default WordPress do not provide the excerpt functionality on pages like it provide on the posts



And on pages it does not show.


To add this feature in your WordPress website add the following code snippet in your theme function file.


function add_excerpt_to_page() {

 add_post_type_support( 'page', 'excerpt' );

}
add_action( 'init', 'add_excerpt_to_page' );


 


Read more