PHP Developer

Saturday, 25 October 2014

Executing php inside a WordPress widget

Executing php inside a WordPress widget without any plugin

Sometimes in your WordPress theme you need to execute custom php code in a widget, because you want to display different information according to the category you are in, or simply because you need to execute a php script into that widget.
There are a lot of plugins doing this task, adding a new type of widget generally called “php widget”, but rather than installing a plugin this simple job can be done simply adding in functions.php file of your theme these few lines:

/*-------------------------  START -------------------------------------*/
add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false){
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}

/*---------------------------- END ----------------------------------*/ 

Which will turn the default Text widget into a php enabled widget.
Adding this feature directly to functions.php allows you to create a theme with this built in feature without the need of an external plugin.

How to Display Recent Posts From A Specific Category In WordPress

How to Display Recent Posts From A Specific Category In WordPress


<?php
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li>
    <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>


At the top where it says showpost= change the number to how many posts
you want to display, and cat=3 is the id of the category, so change the
ID of the category to pick which category will you be displaying.

How To Get Custom Meta Box Value in WordPress

 How To Get Custom Meta Box Value in WordPress


<?php $id = post_id;?>  //Post id save in $id
<?php echo get_post_meta($id, '
origin', true );?>   // Get value of Metabox with meta_key name origin

<?php echo get_post_meta($id, 'preparation_time', true );?>   // Get value of Metabox with meta_key name preparation_time
<?php echo get_post_meta($id, 'amount_of_persons', true );?>   // Get value of Metabox with meta_key name amount_of_persons
<?php echo get_post_meta($id, 'benodigde_ingredienten', true );?>   // Get value of Metabox with meta_key name benodigde_ingredienten
<?php echo get_post_meta($id, 'bereiding', true );?>   // Get value of Metabox with meta_key name bereiding   

How To Create Custom Post With Meta Boxes Plugin In WordPress

How To Create Custom Post With Meta Boxes Plugin In WordPress


<?php
/*
Plugin Name: Recipe Meta Box
Plugin URI:
Description: With the help of this plugin you can add recipe detail where add post or create recipe for your project.
Author: Sunil Sharma
Author URI: https://plus.google.com/u/0/115833273431242118637
*/

$paths = ABSPATH;
include_once $paths.'wp-load.php';

/*--------------------------------Create Custom Post Code Start---------------------------------------*/
function recipe_init() {
    $args = array(
      'public' => true,
      'label'  => 'Recipe Code',
      'supports'  => array( 'title', 'editor', 'thumbnail' )
    );   
    register_post_type( 'recipe', $args );   
}
add_action( 'init', 'recipe_init' );
/*--------------------------------Create Custom Post Code End---------------------------------------*/

Monday, 20 October 2014

Create & Delete table in database at the time of Plugin Activation and Deactivation in WordPress

Create & Delete table in database at the time of Plugin Activation and Deactivation in WordPress

Create a plugin using this code & upload in WordPress and activate it, then check database, new table with name "wp_video_image" is add in database.

<?php
/*
Plugin Name: Create Table in Database
Plugin URI:
Description: This plugin helps you to to Create & Delete table in database at the time of plugin Activation and Deactivation. And also insert the values in plugin activation time.
Version: 0.1
Author: Sunil Sharma
Author URI: https://plus.google.com/u/0/115833273431242118637
*/

$paths = ABSPATH;  //absolute path
if(!defined('DS')){
    define('DS',DIRECTORY_SEPARATOR);
}
include_once $paths.'wp-load.php';
register_activation_hook(__FILE__,'vid_install');  //Function call at plugin activation
register_deactivation_hook(__FILE__ , 'vid_uninstall' );  // Function call at plugin Deactivation

Sunday, 19 October 2014

How To Create Custom User Role or User type in Wordpress

How To Create Custom User Role in Wordpress


<?php add_role( $role, $display_name, $capabilities ); ?>

Example :
add_role('basic_contributor', 'Basic Contributor', array(
               'read' => true, // True allows that capability
               'edit_posts' => true,
               'delete_posts' => false, // Use false to explicitly deny
));
 

Thursday, 16 October 2014

How To Create A WordPress Custom Widget


How To Create A WordPress Custom Widget


Steps to Follow

I have created a new folder named, wordpress_widget where I have saved a newly created file widget.php.

Now, save this wordpress_widget folder under the plugin folder of WordPress.


(Eg. wamp -> www -> wordpress -> wp-content -> plugins)

1. Enter Plugin Details

When you create a new plugin, it is very necessary to provide all its details like- plugin name, plugin URI, its short description, author name, author URI etc. You can follow the below code and enter details according to it.