Pages

Wednesday, September 19, 2012

Get first video or Image from post [resolved]

<?php

function show_thumb() {
    global $post, $posts;
    $thumb = '';
    ob_start();
    ob_end_clean();
    // filter video
    $output = preg_match_all('/(\<object.*\<\/object\>)/is', $post->post_content, $matches);
    if($output == ''){    $output = preg_match_all('/(\<iframe.*\<\/iframe\>)/is', $post->post_content, $matches);    }
    if($matches[1][0] != '')    {    $thumb = '<div style="float:left; margin-right:10px;border: 1px solid #cccccc; padding: 2px;">'.$matches[1][0].'</div>';} else { $thumb = ''; }
    if(empty($thumb)) { // no video tags found, filter wordtube tag instead
        $output = preg_match_all('/\[media id=(.*?)]/i', $post->post_content, $matches);
        $thumb = $matches[1][0];
        if (empty($thumb)) { // no wordtube tags, search for images instead
            $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
            $thumb = $matches[1][0];
            if(empty($thumb)) { // no images found, use a default image
                $postlist_id = $post->ID;
                    $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $postlist_id ), "medium");
                    $myimage = $imgsrc[0];
                    if($myimage != '')
                    {
                        $thumb = '<div style="float:left; margin-right:10px;"><img src="'.$myimage.'" alt="'.$post->post_title.'" title="'.$post->post_title.'" style="width:150px; border: 1px solid #cccccc; padding: 2px;"/></div>';
                    }
            } else {  // image found, return image
                $thumb = '<div style="float:left; margin-right:10px;"><img src="'.$thumb.'" alt="'.$post->post_title.'" title="'.$post->post_title.'" style="width:150px; border: 1px solid #cccccc; padding: 2px;"/></div>';
            }
        }else { //  wordTube tag found, execute shortcode
            $thumb = '[media id=' . $thumb . ']';
            $thumb = apply_filters('the_content', $thumb );
        }
    }
    return $thumb;
}

?>
<?php    echo $myimage_name = show_thumb();    ?>


Wordpress create new Post type [resolved]

<?php


function codex_custom_init() {
  $labels = array(
    'name' => _x('Specials', 'post type general name'),
    'singular_name' => _x('Special', 'post type singular name'),
    'add_new' => _x('Add New', 'special'),
    'add_new_item' => __('Add New Special'),
    'edit_item' => __('Edit Special'),
    'new_item' => __('New Special'),
    'all_items' => __('All Specials'),
    'view_item' => __('View Special'),
    'search_items' => __('Search Specials'),
    'not_found' =>  __('No specials found'),
    'not_found_in_trash' => __('No specials found in Trash'),
    'parent_item_colon' => '',
    'menu_name' => __('Specials')

  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'page',
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','page-attributes' )
  );
  register_post_type('special',$args);
}
add_action( 'init', 'codex_custom_init' );

?>

Tuesday, September 18, 2012

Install MSSQL to wamp server [Resolved]


Install lower version of wamp server (Like 5.2.2)

After installing wamp server

Open php.ini file than activate below extension
from ;extension=php_mssql.dll to extension=php_mssql.dll


and than restart wamp server


now create one testmassql.php file in www directory
and
write blow code

<?php

    if (function_exists('mssql_connect'))
    {
        echo "Okay, fn is there";
    }
    else
    {
        echo "Hmmm .. fn is not even there";
    }

?>





Tuesday, September 4, 2012

Add New Options To Wordpress General Settings


add_filter('admin_init', 'general_settings_register_fields');

function general_settings_register_fields()
{
    register_setting('general', 'test_field', 'esc_attr');
    add_settings_field('test_field', '<label for="test_field">'.__('Test Field' , 'test_field' ).'</label>' , 'general_settings_fields_html', 'general');
}

function general_settings_fields_html()
{
    $value = get_option( 'test_field', '' );
    echo '<input type="text" id="test_field" name="test_field" value="' . $value . '" />';
}

Get the first image or video from a post - [resolved]


The function returns an image/video and must be called inside the loop.

function show_thumb() {
    global $post, $posts;
    $thumb = '';
    ob_start();
    ob_end_clean();
    // filter video
    $output = preg_match_all('/(\<object.*\<\/object\>)/is', $post->post_content, $matches);
    if($output == ''){    $output = preg_match_all('/(\<iframe.*\<\/iframe\>)/is', $post->post_content, $matches);    }
    if($matches[1][0] != '')    {    $thumb = $matches[1][0];} else { $thumb = ''; }
    if(empty($thumb)) { // no video tags found, filter wordtube tag instead
        $output = preg_match_all('/\[media id=(.*?)]/i', $post->post_content, $matches);
        $thumb = $matches[1][0];
        if (empty($thumb)) { // no wordtube tags, search for images instead
            $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
            $thumb = $matches[1][0];
            if(empty($thumb)) { // no images found, use a default image
                $postlist_id = $post->ID;
                    $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $postlist_id ), "medium");
                    $myimage = $imgsrc[0];
                    if($myimage != '')
                    {
                        $thumb = '<img src="'.$myimage.'" alt="'.$post->post_title.'" title="'.$post->post_title.'" style="width:150px; border: 1px solid #cccccc; padding: 2px;"/>';
                    }
            } else {  // image found, return image
                $thumb = '<img src="'.$thumb.'" alt="'.$post->post_title.'" title="'.$post->post_title.'" style="width:150px; border: 1px solid #cccccc; padding: 2px;"/>';
            }
        }else { //  wordTube tag found, execute shortcode
            $thumb = '[media id=' . $thumb . ']';
            $thumb = apply_filters('the_content', $thumb );
        }
    }
    return $thumb;
}