Menu bar

Wednesday 30 December 2015

Compress css within PHP using PHP

COMPRESS CSS



Create a style.php file, not style.css.
paste this code.


<?php ob_start ("ob_gzhandler"); header("Content-type: text/css; charset: UTF-8"); header("Cache-Control: must-revalidate"); $offset = 60 * 60 ; $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT"; header($ExpStr); ?> body { color: red; }
css goes here as always...

Then call this stylesheet with php file name, see below...thats it.

<link rel='stylesheet' type='text/css' href='css/style.php' />
string ob_gzhandler ( string $buffer , int $mode )

ob_gzhandler() is intended to be used as a callback function for ob_start() to help facilitate sending gz-encoded data to web browsers that support compressed web pages. Before ob_gzhandler() actually sends compressed data, it determines what type of content encoding the browser will accept ("gzip", "deflate" or none at all) and will return its output accordingly. All browsers are supported since it's up to the browser to send the correct header saying that it accepts compressed web pages. If a browser doesn't support compressed pages this function returns FALSE.

How to show or hide widgets on specfic pages?

The current sample is setup to only display the core ‘pages’ widget on the contact page only. Don’t forget that is_page also accepts an array of page names and id’s.


add_filter( 'widget_display_callback', 'hide_widget_pages', 10, 3 );

function hide_widget_pages( $instance, $widget, $args ) {
  
if ( $widget->id_base == 'pages' ) { // change 'pages' to widget name
     
if ( !is_page( 'contact' ) ) {    // change page name
         
return false;
     
}
  
}
}

Wordpress interview questions and answers

How to store the post title in variable?
  $page_title = get_the_title($post->post_parent);

How to Generates a random password of the specified length in WordPress?
<?php generate_random_password($len) ?>

How to hide the Admin Bar in WordPress?
add_filter('show_admin_bar', '__return_false');

 How to get the postid in wordpress?
global $wp_query;
$postid = $wp_query->post->ID;
echo $postid;

 How to get the post meta value in worpdress?
we can get the post meta value throw postid.
for example here post Custom Field name : comapny name
 get_post_meta($postid, 'Company name', true);


 How to check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() )  // check if the post has a Post Thumbnail assigned to it.
   { 
the_post_thumbnail();
   }


How to get the wordpress featued image and how to change the image width and height.

we can get the featurd image wordpress function

 the_post_thumbnail();//here we can get the wordpress featured image thumbnail
  the_post_thumbnail( array(100,100) );     // here we can change the image size.

How to get the specified category post only in wordpress?
<?php query_posts("cat=106 && per_page=5");// here 106 is the category id  ?>
<?php while ( have_posts() ) : the_post(); ?>
<h3><?php the_title(); // here we can get the post title. this is the wordpress free defind function ?></h3>
<?php  endwhile; ?>

What is the prefix of wordpress tables by default?
 By default, wp_ is prefix of wordpress.

How can we backup or import our WordPress content from admin panel?
 For import content from wordpress admin panel goes to
WordPress admin -> Tools -> Import


Can wordPress use cookies?
Yes, wordpress use cookies.WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users.

How to disable wordpress comment?
 Look in to dashboard under Options –> Discussion. There is a checkbox there for “allow people to post comments on the article” Try unchecking that.

How many tables a default WordPress will have?

 default wordpress will have 11 tables. They are-
1. wp_commentmeta
2. wp_comments
3. wp_links
4. wp_options
5. wp_postmeta
6. wp_posts
7. wp_terms
8. wp_term_relationships
9. wp_term_taxonomy
10.wp_usermeta
11.wp_users

What are the features of wordpress?
1. Simplicity,Make wordpress to manage easily that is its very easy to use.
2. Free open source ,its free to use wordpress.
3. Easy theme system,In wordpress we have thousand of good free theme to use.
4. Extends with plugins, we can extends the functionality of wordpress using thousands of free plugins or will create any plugin according to your requirements.
5. Community,WordPress has vibrant and supportive community.
6. Multilingual, wordpress is available on more than 70 languages.
7. Flexibility, with wordpress you will create any type of blog or website.
8. Comment, the built in comment system also make wordpress popular as you can comment your views on website.
9. Easy installation and upgrades.
10. Full standards compliance, Easy Importing,Cross-blog communication tools.

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>