Showing posts with label wordpress. Show all posts
Showing posts with label wordpress. Show all posts

Saturday, October 6, 2012

Wordpress front end editor

Ever got a requirement of retrieving customer submitted data and storing it into your post, page or custom post type in Wordpress.

Well, there is a simple editor function wp_editor() is available within wordpress.

Monday, September 24, 2012

Move data from one post type to another using SQL

There are many instances where you want to move the data from one post type to newly created post type in wordpress. For this you can use following simple SQL query.

UPDATE wp_posts SET post_type = 'new-post-type' WHERE post_type =  'post'

Wednesday, September 5, 2012

Modify title tag using wp_title filter

Here is a quick snippet to modify title tag for page, post, category using wp_title hook.

<?php
  add_filter('wp_title', 'modify_title', 20);
  function modify_title($title) {
    return $title . ' foo';
  }
?>

Wednesday, August 22, 2012

Wordpress: How to retrieve multiple page content?

Here is a quick snippet to retrieve content from multiple pages and display on any page, post or template. You  can even try creating a short-code for this functionality and could pass page id's to in the argument.

Below, is the screenshot for what I achieved from this code.



<?php
  /*
  Pass the page id's in an array.
  */
  $home_pages = array('11', '13', '16');
  $output =''; // Output variable

foreach($home_pages as $key => $val) {
      $output .= '<div class="box">';
      $page_id = $val; 
      $page_data = get_page( $page_id ); 
      $output .= get_the_post_thumbnail($page_id);
      $page_link = $page_data->guid; //This for SEF permalinks
      $page_link = explode('/?', $page_link);
      $page_link = $page_link[0];
      $page_link = $page_link . '/' . $page_data->post_name;
      $output .= '<h3>'. $page_data->post_title .'</h3>';
      $content = apply_filters('the_content', $page_data->post_content); 
      $content = substr($content, 0 , 100);
      $output .= $content . '...';
      $output .= '<br /><a href="'.$page_link.'" title="Read More">Read More...</a>';
      $output .= '</div>';
 }
 
 echo $output;
?>

Monday, August 13, 2012

Ultimate shortcode for displaying custom post types

Here you go, I made this shortcode to display any custom post type within your post or page. You can also use do_shortcode function to use this shortcode inside your php file.

Insert the following code inside your theme's functions.php and let the wordpress know about this newly created shortcode by add_shortcode() function.

Wordpress - shortcode for current theme path

You can find this short code useful when inserting some file or image from the relative path of your theme. Simply copy and paste following code inside your theme's functions.php and use the short code in your Page/ Post editor.

<?php
function currentThemePath() {
return get_option('home') . '/wp-content/themes/activeThemeName/';
}
add_shortcode('myThemePath', 'currentThemePath');
?>

Replace 'activeThemeName' with your current active theme name.
And use the short code [myThemePath] in your page/ post.

Friday, August 10, 2012

Wordpress - eliminate non-ASCII characters

It is usual that you get special characters (or non-ASCII characters) when you try to fetch RSS feeds and display on your blog. You can easily filter it using PHP preg_replace function.

Inside a Wordpress's Post query loop, store the the content into a variable and replace all non- ASCII Characters using following code.


 <?php while (have_posts()) : the_post(); ?>
   $the_content = get_the_content();
   $the_content1 = preg_replace("/[^(\x20-\x7F)]*/","",$the_content);
   echo $the_content1;
 <?php endwhile; ?>

Thursday, August 9, 2012

Wordpress Custom Post Type Pagination

Custom post types in wordpress enables developers to do lot of things. But, when it comes to paginating it - lot of people struggle. I searched the web for the solution but could not find proper solution. So, thought of finding it on my own.

So, I created a custom post type called 'projects'.



Friday, August 3, 2012

Wordpress's template hierarchy reference

Found nice reference for wordpress's template hierarchy. This will help to determine the wordpress behavior while developing themes. 

Show all post from custom post type's categories using wp_dropdown_categories() function in Wordpress

Wordpress's custom post type is great way to add content to the site. It gives logical sections to insert different posts instead of using traditional post categories.



In this section, we'll discuss on how to create drop down showing all the categories from a custom post type. And, on selecting particular category from the drop down menu we'll display all the post from that category on the same page. 

Well, I'm not going to use Ajax to display the page. Instead an iframe will work good in our case. You can use Ajax if you want.

Thursday, August 2, 2012

Contact form 7 custom validation

While building a  wordpress theme for one of my client I came across some validation issues for contact form7 where two data must show logical error when check in date is bigger than the check out date. First, thing came into my mind is to edit the plugin itself, but this is not the proper solution because if client updates the plugin then my codes will broken again.

Another, solution was to use contact form 7's hooks. The filter 'wpcf7_validate_text' provides a way to directly send validation errors when your requirement is not fulfilled.