Remove Space or Special characters

so if you have number of string like

$numberString = +1-800-123-(456);

after calling through function clean($numberString);

Result: 1800123456

function clean($string) {
	$string = str_replace(' ', '', $string); // Replaces all spaces.
	$string = preg_replace('/([^A-Za-z0-9\-])/', '', $string); // Removes special chars.
	return preg_replace('/-+/', '', $string); // Replaces multiple hyphens with single one.
}
Advertisements

Get Specific Post by id

Get Post by specific ID

<?php query_posts('p=69'); ?>
	<?php while (have_posts()) : the_post(); ?>
		<?php the_content(); ?>
<?php endwhile;?>

Get Image Src

the_post_thumbnail_url( string|array $size = 'post-thumbnail' )

wp_get_attachment_image_src( $attachment_id, $size, $icon );

wp_get_attachment_image_src( get_post_thumbnail_id(), ‘full’, false )[0];

  • [0] => url
  • [1] => width
  • [2] => height
  • [3] => boolean: true if $url is a resized image, false if it is the original or if no image is available

Widget Area

Untitled-2

Create widget area in theme…

add this code in function.php


function arphabet_widgets_init() {

	register_sidebar( array(
		'name'          => 'Footer Area 1',
		'id'            => 'footer_1',
		'before_widget' => '',
		'after_widget'  => '',
		'before_title'  => '<h5>',
		'after_title'   => '</h5>'
	));
	
	register_sidebar( array(
		'name'          => 'Footer Area 2',
		'id'            => 'footer_2',
		'before_widget' => '',
		'after_widget'  => '',
		'before_title'  => '<h5>',
		'after_title'   => '</h5>'
	));

	register_sidebar( array(
		'name'          => 'Contact Us Widget',
		'id'            => 'contact_us',
		'before_widget' => '',
		'after_widget'  => '',
		'before_title'  => '<h5>',
		'after_title'   => '</h5>'
	));

}

add_action( 'widgets_init', 'arphabet_widgets_init' );

 

To call Widget in theme just paste this code

<?php if ( !function_exists('dynamic_sidebar')) : dynamic_sidebar( 'footer_1' ); endif; ?>

Custom Post Type

posttype

add this code in function.php or create a file in theme inc folder posttypes.php

and add this line in function.php file
require get_template_directory() . ‘/inc/posttypes.php’;

add_action('init', 'posttype_register');
 
function posttype_register() {
 
	$labels = array(
		'name' => 'Post Name',
		'singular_name' => 'Post Singular Name',
		'add_new' => 'Add New',
		'add_new_item' => 'Add New Item',
		'edit_item' => 'Edit Item',
		'new_item' => 'New Item',
		'view_item' => 'View Item',
		'search_items' => 'Search',
		'not_found' => 'Nothing found',
		'not_found_in_trash' => 'Nothing found in Trash',
		'parent_item_colon' => '',
		'menu_name' => 'Post Menu Name'
	);
 
	$args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'query_var' => true,
		'menu_icon' => get_stylesheet_directory_uri() . '/images/icon.png',
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'supports' => array('title','thumbnail')
	  ); 
 
	register_post_type( 'posttypename' , $args );
}

 

Call Custom Post Type Posts

<?php $type = 'posttypename'; $args=array( 'post_type' => $type,
	'post_status' => 'publish',
	'posts_per_page' => 10,
	'caller_get_posts'=> 1);
	
	$my_query = null;
	$my_query = new WP_Query($args);
	
	while ($my_query->have_posts()) : $my_query->the_post(); ?>

<h2><?php the_title(); ?></h2>


<div><?php the_content(); ?></div>


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


	<?php endwhile; wp_reset_query(); ?>

Custom Theme Panel

12-10-2015-3-38-43-PM

You can add this code in function file or create a file name theme-panel.php and add it to your theme in INC folder

add this this line in function.php
require get_template_directory() . ‘/inc/theme-panel.php’;

<?php 
function theme_settings_page()
{
    ?>
	    <div class="wrap">
	    <h1>Theme Panel</h1>
	    <form method="post" action="options.php">
	        <?php
	            settings_fields("section");
	            do_settings_sections("theme-options");      
	            submit_button(); 
	        ?>          
	    </form>
		</div>
	<?php
}

<strong>Adding in left Menu</strong>

function add_theme_menu_item()
{
	add_menu_page("Theme Panel", "Theme Panel", "manage_options", "theme-panel", "theme_settings_page", null, 99);
}

add_action("admin_menu", "add_theme_menu_item");



<strong>Adding Fields</strong>


function display_twitter_element()
{
	?>
    	<input type="text" name="twitter_url" id="twitter_url" value="<?php echo get_option('twitter_url'); ?>" />
    <?php
}

function display_facebook_element()
{
	?>
    	<input type="text" name="facebook_url" id="facebook_url" value="<?php echo get_option('facebook_url'); ?>" />
    <?php
}

function display_youtube_element()
{
	?>
    	<input type="text" name="youtube_url" id="youtube_url" value="<?php echo get_option('youtube_url'); ?>" />
    <?php
}


<strong>Display Theme Options</strong>


function display_theme_panel_fields()
{
	add_settings_section("section", "All Settings", null, "theme-options");
	
	add_settings_field("twitter_url", "Twitter Profile Url", "display_twitter_element", "theme-options", "section");
        add_settings_field("facebook_url", "Facebook Profile Url", "display_facebook_element", "theme-options", "section");
	add_settings_field("youtube_url", "Youtube Profile Url", "display_youtube_element", "theme-options", "section");

        register_setting("section", "twitter_url");
        register_setting("section", "facebook_url");
	register_setting("section", "youtube_url");
}

add_action("admin_init", "display_theme_panel_fields");
?>

Html Tag in Title

Suppose if we have to add title like this
<h1>Main Heading</h1><h2>Sub Heading</h2>


1- Add this code in function file:

function your_html_widget_title( $title ) { 
 $title = str_replace( '[', '<', $title );
 $title = str_replace( '[/', '</', $title );
 $title = str_replace( ']', '>', $title );
 return $title;
}

add_filter( 'widget_title', 'your_html_widget_title' );
add_filter( 'title', 'your_html_widget_title' );

 

2- Now replace less than or greater than sign with Square Brackets like this:
[h1]Main Heading[/h1][h2]Sub Heading[/h2]