10 Useful One Line Code for WordPress Developers

I have seen mostly wordpress developers which are beginner in wordpress forget most useful one line codes in wordpress like get template url in wordpress or image path in wordpress and much more and using search engine to find them every time so very time taking for a project. then i think why not i am share all Useful One Line Codes only on one place. And here i am discussed with you about 10+ Useful One Line Code for WordPress Developers.

1. Create Widget Area

For creating a widget area in wordpress copy and paste the code in below in your functions.php file

if (function_exists('register_sidebar')) {
     register_sidebar(array(
      'name' => 'Social Widget Area',
      'id'   => 'social-widget-area',
      'description'   => 'Social Widget Area',
      'before_widget' => '<div id="one" class="two">',
      'after_widget'  => '</div>',
      'before_title'  => '<h2 class="widget-title">',
      'after_title'   => '</h2>'
     ));
    }

2. Display Widget Area Content

<?php dynamic_sidebar( 'social-widget-area' ); ?>

3. Create Custom Page Template in WordPress

Create a php file and paste the code below and save this file in your theme directory

<?php
/*
Template Name: My Custom Page
*/
?>

4. Image Path in WordPress

See the code below to give right path to a image in wordpress

<img src="<?php bloginfo('stylesheet_directory'); ?>/images/yourimage.jpg" />

5. Attach CSS File From CSS Directory in WordPress

See the code below and attach your css file like this use same method for javascript.

<link rel="stylesheet" href="<?php bloginfo('template_directory');?>/css/yourcss.css">

6. Display Page Title in WordPress

use the code below to display your page title

<?php the_title(); ?>

7. Register menu in wordpress

Paste the code below in your functions.php file for register menu in wordpress

function register_my_menus() {
  register_nav_menus(
    array(
      'header-menu' => __( 'Header Menu' ),
      'extra-menu' => __( 'Extra Menu' )
    )
  );
}
add_action( 'init', 'register_my_menus' );

8. Display menu on theme

Below code used for display register menu anywhere in your theme

<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>

9. echo Shortcode in Template

use below one line code for echo your plugins shortcodes in template

<?php echo do_shortcode('[shortcode]'); ?>

10. Get Featured Image In WordPress

This code use for display featured image on your template you can change image size using,(thumbnail, medium, large, full)

<?php the_post_thumbnail( 'full' ); ?>

11. Display date & time

<?php the_time('M.d, Y') ?>

12. Display category name

<?php the_category(', '); ?>

The following two tabs change content below.

I hope you enjoyed it and learned something. Do not forget to backup your wordpress before editing any important documents including functions.php, index.php and header.php. That way, if something goes wrong, you have a fresh copy of your website that you know is working. If you would like to share your ideas or comments, feel free to post your comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *