Menu bar

Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

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>

Monday, 18 August 2014

PHP array functions explained.

PHP array functions.

1. array_change_key_case  -  Changes the case of all keys in an array and not of the values of the array.

Example:
<?php 
$input_array = array("Key1" => 1, "Key2" => 2);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>

Here, in an output ,Key1 and Key2 will be change to KEY1 and KEY2.



2.array_chunk - Split an array into chunks.It will divide the array according to argument value that will be pass in this function.

Example1:
<?php
$chunk_array=array("a","b","c","d","e");
print_r(array_chunk($chunk_array,3));
?>

Example2:
<?php
$chunk_array=array("a","b","c","d","e");
print_r(array_chunk($chunk_array,3,true));
?>


Wednesday, 16 July 2014

Best places to put the keywords for Search Engine Optimizarion!

How to increase your website rank? Through SEO?
Yes..But how to work with it?

SEO especially work with the words.
For that we should know our website relevent words and WHERE TO PUT IT.
So here are the text which indicate where to put your website relevent words.




Here is a list of places where you should try to use your main keywords. 
  1. Keywords in the <title> tag(s). 
  2. Keywords in the <meta name="description"> 
  3. Keywords in the <meta name="keyword"> 
  4. Keywords in <h1> or other headline tags. 
  5. Keywords in the <a href="http://yourcompany.com">keywords</a> link tags.
  6. Keywords in the body copy. 
  7. Keywords in alt tags. 
  8. Keywords in <!-- insert comments here> comments tags. 
  9. Keywords contained in the URL or site address, e.g., http://www.keyword.com/keywordkeyword.htm.


Saturday, 5 July 2014

How to disable right click , F12 key and Direct link access?

Stop users from stealing your website images, copying content, or from inspecting the source.

In order to setup a security as above , the first is to disable right click ,disable F12 key and then disable direct link access.

Direct link access means that a user get full path of image by writing 'view-source:' before the domain name.
After getting the full path, user will type the full path of image in address bar to access images, So we have to avoid this attack also.
Use this code in your header file or where ever you required.

<body oncontextmenu="return false;">
Fires when the user clicks the right mouse button in the client area, opening the context menu.


<meta http-equiv="imagetoolbar" content="no" />
This turns off Internet Explorer's image toolbar that appears when you hover over an image.
Use this code within <head > tag.


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
Write this code in your .htaccess file

Monday, 23 June 2014

Understanding Join Query.

Untitled Document Here we are goingto join two tables with common column named

Emp'Age' Table
EmpAge
Ron White32
Femina65
John25
Don

'Salary_Emp' Table
SalaryEmp
40000Ron White
29000Femina
25000
11000Ron White

The important thing to note here is that the column Emp contains information that can tie these two tables together. In the "Age" table, the Emp column contains all the Employee of the Company and their respective ages. In the "Salary_Emp" table the Emp column contains the respective Salary of the Employee. It's only through a shared column relationship such as this that tables can be joined together, so remember this when creating tables you wish to have interact with each other.
//php start
// Make a MySQL connection using your database credentials
// Construct our join query
$query = "SELECT Age.Emp, Salary_Emp.Salary ".
 "FROM Age, Salary_Emp".
 "WHERE Age.Emp = Salary_Emp.Emp";
  
$result = mysql_query($query) or die(mysql_error());


// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
 echo $row['Emp']. " - ". $row['Salary'];
 echo "
";
}
//php ends
EmpSalary
Ron White40000
Femina29000
Ron White11000