Home / WordPress

WordPress

WordPress Custom Post

WordPress has been gaining a foothold in the general content management system (CMS) game for a few years now, but the real breakthrough was the custom post type mechanism which allows for the creation of a wide variety of content in WordPress. In this plugin Custom Post For WordPress you can easily created your custom post like audio,video and portfolio…etc

==== Installation ====

  • Download Custom Post For WordPress.
  • Upload the Custom Post For WordPress directory to your /wp-content/plugins/ directory, using your favorite method (ftp, sftp, scp, etc…)
  • Activate Custom Post For WordPress from your Plugins page. (You’ll show “WPCustomPost” Menu in your admin sidebar.)
  • Then add new for WPCustomPost
  • After post has been created successfully then add new (post or page), then simply add shortcode [khan] in your post or page, You’ll show your all custompost 🙂

see more here

Fix WordPress Feed XML Parsing Errors

When you try to Export all content or rss feed in wordpress then it’ll show XML Parsing Error and not working with seo in your site and so was the WordPress source feed which showed “XML Parsing Error: XML or text declaration not at start of entity“. And all this while Google reader was correctly tracking our feed, while it was not loading in any web browser!

XML Parsing Error
Firefox showed this error on loading the feed

XML Parsing Error: XML or text declaration not at start of entity Location: http://www.quickonlinetips.com/archives/feed/ Line Number 2, Column 1: ^

Google Chrome revealed some more information

This page contains the following errors: error on line 2 at column 6: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error.

I found that it was most commonly caused by a Blank line before XML declaration

Basically this error arises if there is a whitespace before an XML declaration

<?xml ... ?>

appears in your feed. The blank line error means that the code has blank lines in it, which can be easily introduced in the WordPress php code by a plugin, theme, or any configuration file.

So basically I had to go looking for blank lines before PHP starting tag

<?php
and after the PHP closing tag
 ?>

I intended to use the ‘Fix RSS Feed WordPress plugin’ which promised to search all folders (except wp-admin and wp-includes directory) if the php file has blank lines in the head and tail line, and then delete blank lines. The plugin author writes that it will delete php files at only the beginning and end of the blank lines, and will not delete the blank lines between documents, so it will not affect the operation of the php programs.

Now, change your root directory where wp-config.php include, see below

Rasel Khan:~$ cd /var/www/project/wordpress
Rasel Khan:~$ ls
index.php    wp-activate.php     wp-comments-post.php  wp-content   wp-links-opml.php  wp-mail.php      wp-trackback.php
license.txt  wp-admin            wp-config.php         wp-cron.php  wp-load.php        wp-settings.php  xmlrpc.php
readme.html  wp-blog-header.php  wp-config-sample.php  wp-includes  wp-login.php       wp-signup.php

open your ‘index.php’ file via any editor, I’m using gedit editor

Rasel Khan:~$ sudo gedit index.php

Here ‘index.php’ file look

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

Now create a new file called ‘fix-rss.php’ in ‘wp-config.php’ directory and copy the follwing code

<?php
function wp_xml_fix_rss($input) {
	$allowed = false;

	$found = false;

	foreach (headers_list() as $header) {
		if (preg_match("/^content-type:s+(text/|application/((xhtml|atom|rss)+xml|xml))/i", $header)) {
			$allowed = true;
		}

		if (preg_match("/^content-type:s+/i", $header)) {
			$found = true;
		}
	}

	if ($allowed || !$found) {
		return preg_replace("/As*/m", "", $input);
	} else {
		return $input;
	}
}

ob_start("wp_xml_fix_rss");

Finally include ‘fix-rss.php’ file in ‘index.php’, see below look like

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
include './fix-rss.php';
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

It’ll be works ! 🙂
if any problem just comment here………

Add custom meta box in WordPress

You want to custom meta box add in your theme or plugin? if you want let’s go how to development this meta box, wordpress all functions include your “functions.php” file in your theme and also working with your plugin, open “functions.php” file in your theme and include “add_action()” method

add_action('add_meta_boxes', 'my_meta_box');

here, two parameter pass first one hook name and second one method name, now need to create your custom method

function my_meta_box(){

}

after method create need to meta box initialization look like

function my_meta_box(){

	add_meta_box( 'custom-meta-box', 
		          'Custom Post Metabox',
		           'display_meta_box', 
		           'post',
		           'normal',
		           'low'
		             );
}

If you’re not familiar this method see wordpress Codex for details description !

i’ve third parameter for “display_meta_box” method

 function display_meta_box($post){
  // code here
}

after create this function need to form create for your input field

function display_meta_box($post){
  wp_nonce_field('meta_action', 'meta_box_name_field'); 
	$html = '';
	$html .= '<label for="mp3_title">';
	$html .= 'Add Mp3';
	$html .= '</label>';
	$html .= '<input type="text" name="mp3_title" id="mp3_title" class="widefat" value="' .get_post_meta($post->ID, 'mp3_title', true).'" placeholder="Enter your mp3 title">';
	$html .= '<label for="mp3_file">';
	$html .= 'Mp3 File';
	$html .= '</label>';
	$html .= '<input type="file" name="mp3_file" id="mp3_file">';

	echo $html;
}

I’ve two input field create, when you’ll submit this form you’ll need to save this post, so create save post action, i’m using “save_post”….

add_action('save_post', 'save_meta_box');

now new method name “save_meta_box” for saving post from user, so let’s go create new method

function save_meta_box($post_id){
// Code here
}

after create this method now need to check autopost in wordpress, i’ve create new method name “can_save_post”

function save_meta_box($post_id){
// this method check to autosave
if(can_save_post($post_id, 'meta_box_name_field')){
}

}

Now check this function…

function can_save_post($post_id, $nonce){
	$autosave = wp_is_post_autosave($post_id);
	$revision = wp_is_post_revision($post_id);
    $valid = (isset($_POST[$nonce]) && wp_verify_nonce($_POST[$nonce], 'meta_action'));

    return !($autosave || $revision) && $valid;

}

Finally check “save_meta_box” method from user. see below

function save_meta_box($post_id){
	if(can_save_post($post_id, 'meta_box_name_field')){

        if(isset($_POST['mp3_title']) && 0 < count(strlen(trim($_POST['mp3_title'])))){
				$mp3_title = $_POST['mp3_title'];
				update_post_meta( $post_id, 'mp3_title', $mp3_title);
		}
		if(isset($_POST['mp3_file']) && !empty($_POST['mp3_file'])){

			print_r($_POST['mp3_file']);
			// wp_upload_bits( $name, $deprecated, $bits, $time );

			wp_upload_bits( $_FILES['mp3_file']['name'], null, file_get_contents( $_FILES['mp3_file']['tmp_name']) );
		}
	}
}

now need to display this content, so create new hook “the_content”

add_action('the_content', 'display_meta_box_content');

Finally display this content in front page

function display_meta_box_content($content){

 if(is_single()){
 	$html = 'This is metx box title' . get_post_meta(get_the_ID(), 'mp3_title', true);
 	$html .= 'This is Path url is =' . get_post_meta(get_the_ID(), 'mp3', true);

 	$content .= $html;
 }
return $content;
}

Enjoy it 🙂

Add a color Picker with your WordPress plugin

WordPress comes with the color picker Farbtastic, first head overto download it.

To include the javascript , jscolor.js you can use admin_enqueue_scripts (if you want the color picker to load on the admin side) or use wp_enqueue_scripts (if you want the color picker on the client side). In the example below , I have this loading on the admin side.

//Load Color Picker JS
function jscolor(){
$path = plugins_url('/my-plugin/');
wp_enqueue_script('jscolor',$path. 'jscolor/jscolor.js');
}
add_action('admin_enqueue_scripts','jscolor')

Note that you have to replace my-plugin with the name of the folder for your plugin.
Also the other files for jscolor should be in the same folder as the jscolor.js file.

Now add an input field,

<input class="color {adjust:false}" type="text" name="my_plugin_colorpicker" value="<?php echo $options['my_plugin_colorpicker']; ?>" />

A screenshot of the colorpicker is below:
See