Seach
metabox file upload wordpress code
function aw_include_script() {
if ( ! did_action( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
wp_enqueue_script( 'awscript', get_stylesheet_directory_uri() . '/js/awscript.js', array('jquery'), null, false );
}
add_action( 'admin_enqueue_scripts', 'aw_include_script' );
function aw_custom_meta_boxes( $post_type, $post ) {
add_meta_box(
'aw-meta-box',
__( 'Custom Image' ),
'render_aw_meta_box',
array('slider'), //post types here
'normal',
'high'
);
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
if(isset($_POST['aw_custom_image']))
{
$file = $_POST['aw_custom_image'];
update_post_meta($post_id,'file',$file);
}
}
add_action( 'add_meta_boxes', 'aw_custom_meta_boxes', 10, 2 );
function render_aw_meta_box($post) {
$image = get_post_meta($post->ID, 'file', true);
?>
<table>
<tr>
<td><a href="#" class="aw_upload_image_button button button-secondary"><?php _e('Upload Image'); ?></a></td>
<td><input type="text" name="aw_custom_image" id="aw_custom_image" value="<?php echo $image; ?>" /></td>
</tr>
</table>
<script>
jQuery(function($){
$('body').on('click', '.aw_upload_image_button', function(e){
e.preventDefault();
var button = $(this),
aw_uploader = wp.media({
title: 'Custom image',
button: {
text: 'Use this image'
},
multiple: false
}).on('select', function() {
var attachment = aw_uploader.state().get('selection').first().toJSON();
$('#aw_custom_image').val(attachment.url);
})
.open();
});
});</script>
<?php
}
customizer example
<?php
//https://maddisondesigns.com/2017/05/the-wordpress-customizer-a-developers-guide-part-1/
function diwp_theme_customize_options( $wp_customize)
{
$wp_customize->add_panel( 'diwp_option_panel', array(
'title' => 'Contact info',
'priority' => 0,
'capability' => 'edit_theme_options',
'theme_supports' => '',
'active_callback' => ''
));
$wp_customize->add_section( 'diwp_general_option_section', array(
'title' => 'Contact options',
'panel' => 'diwp_option_panel',
'capability' => 'edit_theme_options',
'theme_supports' => '',
'active_callback' => '',
'description_hidden' => false
));
$wp_customize->add_setting( 'diwp_site_title', array(
'default' => ' ',
'type' => 'theme_mod',
'transport' => 'refresh',
'theme_supports' => ''
));
$wp_customize->add_control( 'diwp_site_title', array(
'label' => 'Phone Number',
'description' => 'Add Phone Number',
'section' => 'diwp_general_option_section',
'type' => 'text',
'input_attrs' => array( 'placeholder' => 'Enter Site Title' )
));
$wp_customize->add_setting( 'diwp_email', array(
'default' => ' ',
'type' => 'theme_mod',
'transport' => 'refresh',
'theme_supports' => ''
));
$wp_customize->add_control( 'diwp_email', array(
'label' => 'Email Addres',
'description' => 'Add Email',
'section' => 'diwp_general_option_section',
'type' => 'text',
'input_attrs' => array( 'placeholder' => 'Enter Site Title' )
));
$wp_customize->add_setting( 'diwp_working_hours', array(
'default' => ' ',
'type' => 'theme_mod',
'transport' => 'refresh',
'theme_supports' => ''
));
$wp_customize->add_control( 'diwp_working_hours', array(
'label' => 'Working hours',
'description' => 'Add Working hours',
'section' => 'diwp_general_option_section',
'type' => 'text',
'input_attrs' => array( 'placeholder' => 'Enter Site Title' )
));
$wp_customize->add_section( 'diwp_general_option_socialmedia', array(
'title' => 'Social media contact',
'panel' => 'diwp_option_panel',
'capability' => 'edit_theme_options',
'theme_supports' => '',
'active_callback' => '',
'description_hidden' => false
));
$wp_customize->add_setting( 'facebook', array(
'default' => ' ',
'type' => 'theme_mod',
'transport' => 'refresh',
'theme_supports' => ''
));
$wp_customize->add_control( 'facebook', array(
'label' => 'Facebook',
'description' => 'Add Facebook address',
'section' => 'diwp_general_option_socialmedia',
'type' => 'text',
'input_attrs' => array( 'placeholder' => 'add facebook address' )
));
$wp_customize->add_setting( 'twitter', array(
'default' => ' ',
'type' => 'theme_mod',
'transport' => 'refresh',
'theme_supports' => ''
));
$wp_customize->add_control( 'twitter', array(
'label' => 'Twitter',
'description' => 'Add twitter address',
'section' => 'diwp_general_option_socialmedia',
'type' => 'text',
'input_attrs' => array( 'placeholder' => 'add twitter address' )
));
$wp_customize->add_setting( 'youtube', array(
'default' => ' ',
'type' => 'theme_mod',
'transport' => 'refresh',
'theme_supports' => ''
));
$wp_customize->add_control( 'youtube', array(
'label' => 'Twitter',
'description' => 'Add youtube address',
'section' => 'diwp_general_option_socialmedia',
'type' => 'text',
'input_attrs' => array( 'placeholder' => 'add youtube address' )
));
}
add_action( 'customize_register', 'diwp_theme_customize_options' );
how to create a widget using code wordpress
<?php
// Creating the widget
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
echo $title;
?>
<input type="text" class="widefat"><input type="button" id="btn">
<script>
(function($)
{
$("#btn").bind('click',function(e)
{
alert();
});
}(jQuery));
</script>
<?php
// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
<textarea name="<?php echo $this->get_field_name( 'textarea' ); ?>" class="widefat">
<?php echo esc_attr( $instance['textarea']); ?>
</textarea>
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['textarea'] = $new_instance['textarea'];
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
// Class wpb_widget ends here
}
// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
short code plugin in WordPress
<?php
/*
Plugin Name: shortcode
Plugin URI: https://hhiitthhiinn.blogspot.in
Version: 1.0
Author: Hithin
Author URI: https://hhiitthhiinn.blogspot.in
*/
function code($atts) {
$default = array
(
'type' => 'post',
'count' => -1,
'categoryid' => '',
'order' => 'ASC',
'taxonomy' => ''
);
$attr = shortcode_atts($default,$atts);
$type = $attr['type'];
$count = $attr['count'];
$categoryid =$attr['categoryid'];
$taxonomy = $attr['taxonomy'];
$args = array(
'posts_per_page' => $count,
'offset' => 0,
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => $type,
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true,
'tax_query' => array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' =>$categoryid
)
);
$posts = get_posts( $args );
foreach($posts as $key=>$item)
{
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $item->ID ), 'thumbnail' );
$image = $image[0];
?>
<style>
.item {
float: left;
border: 1px double #ccc;
margin: 5px !important;
}
</style>
<div class="slick-slide item slick-cloned" data-slick-index="-3" aria-hidden="true" tabindex="-1" >
<figure class="slick-slide-inner">
<a target="_blank" href="<?php echo get_post_permalink($item->ID); ?>"><img class="slick-slide-image" src="<?php echo $image; ?>" alt="poster_5_up"></a>
</figure>
</div>
<?php
}
?>
<?php
}
add_shortcode('code', 'code');
?>
[code parmeters (categoryid,taxonamy,count,order) ]
how to upgrade wordpress core and plugins
- download new WordPress files and delete htaccess,wp-content,wp-config.php from it
- the go to the old WordPress site remove all files and folders except wp-config.php,.htaccess,wp-content from it
- the copy and past it
create a custom theme settings in drupal7
theme_form_system_theme_settings_alter to add a settings for to default settings page
function hithin_form_system_theme_settings_alter(&$form, $form_state)
{
$form["socialmedia"]=array
(
"#type"=>"fieldset",
"#title"=>t("Socialmedia"),
"#collapsible"=>TRUE,
"#collapsed"=>TRUE
);
}
read formapi for further details of the form
create theme options in wordpress
<?php
function create_theme_option()
{
$menu_slug=$page_title="themeoptions";
$menu_title="Theme Options";
$capablity="manage_options";
$callback="create_options";
// $callback=plugins_url()."/themeoptions/home.php";
$menuicon="dashicons-admin-generic";
$position=50;
add_menu_page($page_title,$menu_title,$capablity,$menu_slug,$callback,$menuicon,$position);
}
add_action("admin_menu","create_theme_option");
function create_options()
{
?>
<style type="text/css">
.theme-options input[type=text]
{
height:50px;
font-size:20px;
}
</style>
<h1>Theme options</h1>
<form method="post" action="options.php" class="theme-options" >
<?php
settings_fields("Socialmedia");
do_settings_sections("theme-options");
settings_fields("footer");
submit_button();
?>
</form>
<?php
}
function footer()
{?>
<input type="text" name="footer" id="footer" class="widefat" value="<?php echo get_option('facebook'); ?>" >
<?php }
function facebook()
{?>
<input type="text" class="widefat" id="facebook" value="<?php echo get_option('facebook'); ?>" name="facebook" >
<?php
}
function linkedin()
{?>
<input type="text" class="widefat" id="linkedin" value="<?php echo get_option('linkedin'); ?>" name="linkedin" >
<?php }
function settings()
{
add_settings_section("Socialmedia", "Social Media", null, "theme-options");
add_settings_section("footer", "Footer", null, "theme-options");
add_settings_field("facebook", "Facebook Link", "facebook", "theme-options", "Socialmedia");
register_setting("Socialmedia", "facebook");
add_settings_field("linkedin", "Linkedin Link", "linkedin", "theme-options", "Socialmedia");
register_setting("Socialmedia", "linkedin");
add_settings_field("footer", "Footer Text", "footer", "theme-options", "footer");
register_setting("Socialmedia", "footer");
}
add_action("admin_init", "settings");
create a widget area in your current theme
<?php
function widget_init()
{
register_sidebar( array(
'name' => esc_html__( 'Footer Widget Area 1', 'shopbiz-child' ),
'id' => 'footer_widget_area-1',
'description' => '',
'before_widget' => '<div id="%1$s" class="col-md-3 col-sm-6 rotateInDownLeft animated ta-widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h6>',
'after_title' => '</h6>',
) );
}
add_action('widgets_init','widget_init');
create a custom widget in wordpress
1:creating widget as plugin
2:add code to functions.php
Here i created widget as plugin
basic skelton
class wiget_name extends Wp_widget
{
function widget()
{
parent::WP_Widget(false, $name = __('widget name', 'wp_widget_plugin') );
}
public function update($new,$old)
{
//updates the widget field
}
public function form($instance)
{
//ui creation
}
public function widget($arg,$instance)
{
//front end display
}
}
php ajax wordpress example
create a plugin and add ajax.php
<?php
/*
* Plugin Name:ajax
*/
function my_ajax_callback_function() {
echo "hiii";
exit();
}
add_action( 'wp_ajax_my_action_name', 'my_ajax_callback_function' );
// If called from admin panel
add_action( 'wp_ajax_nopriv_my_action_name', 'my_ajax_callback_function' );
wordpress as webservice
https://wordpress.org/plugins/json-api/
goto plugin->json api ->controller create gallery.php
<?php
/*
Controller name: Gallery
Controller description: Retrieve gallary posts
*/
class JSON_API_Gallery_Controller {
function gallery_posts()
{
$args=array
(
"post_type"=>"gallery",
"post_status"=>"publish",
"orderby"=>"ID"
);
$posts=get_posts($args);
$json=array();
if(count($posts)!=0)
{
$json['status']=array('ok');
foreach($posts as $key=>$record)
{
$youtube=get_post_meta($record->ID,'youtube',true);
$json["result"][]=array("title"=>$record->post_title,'content'=>$record->post_content,'url'=>$youtube);
}
return $json;
}
else
{
return $json['status']=array('failed');;
}
}
}
this fetch gallery named post data
jquery for fetch the service
function gallery_load()
{
$.ajax(
{
url: "http://faithsolution.byethost17.com.in/?json=gallery.gallery_posts",
type: "POST",
dataType: "jsonp",
headers: {"'Access-Control-Allow-Origin": "*"},
beforeSend: function(xhr) {
}, complete: function(jqXHR, textStatus) {
}, success: function(data)
{
if (data.status == "ok")
{
for (i = 0; i < Object.keys(data.result).length; i++)
{
}
}
else
{
}
}, error: function(jr, textstatus)
{
console.log(textstatus);
}});
}
create a custom metabox in wordpress
//additional info reffer https://developer.wordpress.org/plugins/metadata/creating-custom-meta-boxes/
function create_metabox_from($post)
{
$data=get_post_meta( $post->ID,"message");//receiving saved data
?>
<input type="text" name="message" id="message" class="widefat" value="<?php echo $data[0];?>">
<?php
}
function create_metabox()
{
$screen=array("Samples");//it is the post which this meta box need to be apperare
foreach($screen as $key)
{
add_meta_box("sample_metabox","message","create_metabox_from",$screen,"");
}
}
add_action("add_meta_boxes","create_metabox");//creating metabox
add_action( 'save_post', 'save_data' );//saving metabox data
function save_data($post_id)
{
if(array_key_exists("message",$_POST))
{
update_post_meta($post_id,"message",$_POST['message']);
}
}
custom post type in wordpress
*
* create a folder named sample under the plugin directory in wordpress wp-content/plugins/ and put the sample.php to it 100% working
*/
<?php
/*
* Plugin Name:Sample
* Description:Test purpose
* Plugin URI:http://hhiitthhiinn.blogspot.com
* Author:Hithin chandran
* Version:1.0
* AUTHOR URI:http://hhiitthhiinn.blogspot.com
*/
function my_custom_post_product() {
$labels = array(
'name' => _x( 'Samples', 'post type general name' ),
'singular_name' => _x( 'Sample', 'post type singular name' ),
'add_new' => _x( 'Add New', 'Sample' ),
'add_new_item' => __( 'Add New Sample ' ),
'edit_item' => __( 'Edit Sample' ),
'new_item' => __( 'New Sample' ),
'all_items' => __( 'All Samples' ),
'view_item' => __( 'View samples' ),
'search_items' => __( 'Search Samples' ),
'not_found' => __( 'No samples found' ),
'not_found_in_trash' => __( 'No samples found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Samples'
);//label settings
$args = array(
'labels' => $labels,
'description' => 'Holds our Samples and sample specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments','custom-fields','trackbacks',"page-attributes","post-formats" ),
'has_archive' => true,
"slug"=>"sample",
"show_in_admin_bar"=>true,
"menu_icon"=>"dashicons-smiley",
"can_export"=>TRUE
);//custom post type attributes
register_post_type( 'product', $args );//used to register a post type
}
add_action( 'init', 'my_custom_post_product' );//adding the post type
//visit https://codex.wordpress.org/Function_Reference/register_post_type for more information