Menu bar

Sunday 18 January 2015

Convert Hex to RGB

CONVERT HEX TO RGB


<html>
<body>
<form method="post" action="#">
<input type="text" name="hexcolor">
<input type="submit" value="RGB" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
print_r (hex2rgb($_POST['hexcolor']));
}
function hex2rgb( $colour ) {
        if ( $colour[0] == '#' ) {
                $colour = substr( $colour, 1 );
        }
        if ( strlen( $colour ) == 6 ) {
                list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
        } elseif ( strlen( $colour ) == 3 ) {
                list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
        } else {
                return false;
        }
        $r = hexdec( $r );
        $g = hexdec( $g );
        $b = hexdec( $b );
        return array( 'red' => $r, 'green' => $g, 'blue' => $b );
}

Wednesday 14 January 2015

Publish the post if and only if the featured image is set.

SET FEATURED IMAGE AND PUBLISH IT


Dont allow the user to publish the post until the featured image is set.

sometimes after publsihing the post user come to know that featured image is not set. and he/she will try to add featured image once publsihed.<br />
How it look if the post was unable to publish until the featured image is set?

yes, you can do this with wordpress hook.
you have to use "save_post" hook to check whether the post has thumbnail i.e. the featured image is set or not and you have to use "admin_notices" hook to display message above post title.

place this snippet in your functions.php file

add_action('save_post', 'check_thumbnail');
add_action('admin_notices', 'thumbnail_error');

function check_thumbnail($post_id) {
    if(get_post_type($post_id) != 'post')
        return;
    
    if ( !has_post_thumbnail( $post_id ) ) {
        // set a transient to show the users an admin message
        set_transient( "has_post_thumbnail", "no" );
        // unhook this function so it doesn't loop infinitely and it will not publish until featured image is set.
        remove_action('save_post', 'check_thumbnail');
        // update the post and set it to draft
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
        add_action('save_post', 'check_thumbnail');
    } else {
        delete_transient( "has_post_thumbnail" );
    }
}

function thumbnail_error()
{
    if ( get_transient( "has_post_thumbnail" ) == "no" ) {
        echo "<div id='message' class='error'><p><b style="color:#F00;">Select Featured Image and Publish it.</b></p></div>";
        delete_transient( "has_post_thumbnail" );
    }

}

Saturday 10 January 2015

Wordpress Plugins list for post meta

Excellent plugin for Meta Data


  1. Advanced Custom Fields
    This is an excellent plugin to help you add custom metaboxes to save your metadata. The user interface is very polished and all metaboxes are created from right within the WordPress Admin.
  2. Pods - Custom Content Types and Fields
    This is another excellent plugin, but is built with developers in mind. If you need to build something very advanced, and want to manage it from the WordPress dashboard, I recommend you dive into Pods. The developers are very responsive, and the documentation is excellent.
  3. Types - Custom Fields and Custom Post Types Management
    Another advanced solution that will do a lot more than meta-data related fields.
  4. Custom Metaboxes and Fields for WordPress
    This is a library that lets you manage your metabox configuration all in one place via a filter hook. Originally created by Jared Atchison and maintained by Bill Erickson and Andrew Norcross, It's development has been taken over by the crew at WebDevStudios* and is in active development. Definitely check out the wiki for in-depth examples.
  5. Custom Metadata Manager for WordPress
    Another recommended library and is by the fine folks at Automattic, the people behind wordpress.com, so you know you're in good hands.
  6. HM Custom Meta Boxes for WordPress
    Another "CMB" flavor, that was originally forked from Jared Atchison's Custom Metaboxes and Fields for WordPress. It's unique in that it has "a basic layout engine for fields, allowing you to align fields to a simple 12 column grid", among other things.
  7. Fieldmanager
    Fieldmanager is a toolkit for developers to create complex administration screens in WordPress.
    Another advanced toolkit recommended by the folks at Automattic.

Sunday 4 January 2015

Working with custom fields

USING CUSTOM FIELDS


This function returns the values of the custom fields with the specified key from the specified post. It is a wrapper for get_metadata('post').

<?php $meta_values = get_post_meta( $post_id, $key, $single ); ?>


$key is a string containing the name of the meta value you want.


If only $post_id is set it will return all meta values in an associative array.
If $single is set to false, or left blank, the function returns an array containing all values of the specified key.
If $single is set to true, the function returns the first value of the specified key (not in an array)


Dump out all custom fields as a list
<?php the_meta(); ?>

Display value of one specific custom field
<?php echo get_post_meta($post->ID, 'key', true); ?>
"key" would be ID value of custom field


Display multiple values of same custom field ID
<?php $key_val = get_post_meta($post->ID, 'key_val', false); ?>
<h3>This post inspired by:</h3>
<ul>
<?php foreach($key_val as $key) {
echo '<li>'.$key.'</li>';
} ?>
</ul>


Display custom field only if exists
<?php 
    $url = get_post_meta($post->ID, 'reference-URL', true); 

if ($url) {
   echo "<p><a href='$url'>URL</a></p>";
}
?>

Friday 2 January 2015

PHP CURL-Request to check, if a website exists / is online

PHP CURL-REQUEST TO CHECK, IF A WEBSITE EXISTS/IS ONLINE.


<?php
if(isset($_POST['submit'])){
       if (isDomainAvailible($_POST['url']))
       {
              echo "Up^^^^^^^^^^^^^";
       }
       else
       {
               echo "Down__________";
       }
         
}
 function isDomainAvailible($domain)
       {
               //check, if a valid url is provided
               if(!filter_var($domain, FILTER_VALIDATE_URL))
               {
                       return false;
               }

               //initialize curl
               $curlInit = curl_init($domain);
               curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
               curl_setopt($curlInit,CURLOPT_HEADER,true);
               curl_setopt($curlInit,CURLOPT_NOBODY,true);
               curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

               //get answer
               $response = curl_exec($curlInit);

               curl_close($curlInit);

               if ($response) return true;

               return false;
       }
?>
<html>
<body>
<form action="#" method="post">
<input type="url" name="url" >
<input type="submit" value="Check" name="submit">
</form>
</body>
</html>