Thursday, December 6, 2012

How to get the first link in wordpress posts

How to get the first link in wordpress posts

Please follow the code structure to get the first link from a post.


To make this happen just paste this code into your functions.php file.

function get_content_link( $content = false, $echo = false ){
    if ( $content === false )
        $content = get_the_content();

    $content = preg_match_all( '/hrefs*=s*["']([^"']+)/', $content, $links );
    $content = $links[1][0];

    if ( empty($content) ) {
        $content = false;
    }

    return $content;
}

The above function finds the first link in the post and returns that link to you. So In this following way, we can link the title(or whatever) to this place, as describe below:

<h2><a href="<?php echo get_content_link( get_the_content() ); ?>"><?php the_title(); ?></a></h2>

Tuesday, November 27, 2012

How to create a Pinterest "pin it" button for your WordPress blog

How to create a Pinterest "pin it" button for your WordPress blog

Just paste the following code where you like the "Pin It" button to be displayed. But remeber that this code must be inserted within the loop.

<a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' ); echo $thumb['0']; ?>&description=<?php the_title(); ?>" class="pin-it-button" count-layout="horizontal">Pin It</a>

Now footer.php file and add the pinterest JavaScript code:

<script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script>

Thursday, November 22, 2012

Automatic “Nofollow” for external links in post content of wordpress

Automatic “Nofollow” for external links in post content of wordpress

WordPress hack: Automatic “Nofollow” for external links in post content of wordpress

WordPress does not automatically by default add a rel=”nofollow” attribute to its external links within the post content. But If you want to add this attribute, the following solution help you to force WordPress to add the rel=”nofollow” attribute to every external link.

To make this work just Paste the following code in the functions.php file. Now save the file, you can see that all external links in your post content will be changed to nofollow.

add_filter('the_content', 'auto_nofollow');

function auto_nofollow($content) {
    //return stripslashes(wp_rel_nofollow($content));

    return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content);
}

function auto_nofollow_callback($matches) {
    $link = $matches[0];
    $site_link = get_bloginfo('url');

    if (strpos($link, 'rel') === false) {
        $link = preg_replace("%(href=S(?!$site_link))%i", 'rel="nofollow" $1', $link);
    } elseif (preg_match("%href=S(?!$site_link)%i", $link)) {
        $link = preg_replace('/rel=S(?!nofollow)S*/i', 'rel="nofollow"', $link);
    }
    return $link;
}

How to remove WordPress version number from pages and feeds

How to remove WordPress version number from pages and feeds

WordPress always publicly display wordpress version number in RSS and Atom feeds by default. With the following solution we can remove those wordpress version number from all our blog feeds.

You need to just use this code in functions.php file

add_filter('the_generator', 'digwp_complete_version_removal');
function digwp_complete_version_removal() {
    return '';
}

Sunday, October 28, 2012

Search within a specific post type only in Wordpress

Search within a specific post type only in Wordpress

In wordpress, when a user/visitor search website using WordPress built-in search function, wordpress always search through all post types. But sometimes we may need to be able to search only within a wordpress specific post type.The following code example will help to display that.


In the following code allows to modify WordPress search engine to search only within a specific post type. To work like that Just update the code with your post type name on line 4, then paste it into your theme functions.php file.


function SearchFilter($query) {
  if ($query->is_search) {
    // Insert the specific post type you want to search
    $query->set('post_type', 'feeds');
  }
  return $query;
}

// This filter will jump into the loop and arrange our results before they're returned
add_filter('pre_get_posts','SearchFilter');

 

Friday, September 14, 2012

Get post content by ID in Wordpress

When we building a custom theme in wordpress, sometimes we need to get the title or content from a specific post in wordpress. Also sometimes, using a custom loop is the better option for us, but its hard when we need to get information from a specific post only,follow the instruction to overcome that



Just use the get_post_field() function. You can get the post content from it. Fine the following example which will display the content of the post with id $post_id:

echo get_post_field('post_content', $post_id);

Also, the function can be used to get other fields from the post. See the codex page for reference in wordpress.

Tuesday, August 21, 2012

How to create a dropdown menu of tags in wordpress

How to create a dropdown menu of tags in wordpress

Like wp_dropdown_categories() function which display the categories in a dropdown menu, there’s no built in function to display dropdown menu of tags in wordpress. But to display tags in a dropdown as well, Let’s use wp_dropdown_categories() and modify it in order to be able to display tags in a dropdown menu. Just paste the following code where you want the dropdown menu to be displayed. Here we can use our own taxonomy: Just modify the code on line 5 according to your needs.


<h2><?php _e('Posts by Tags'); ?></h2>
<form action="<?php bloginfo('url'); ?>/" method="get">
<div>
<?php
$select = wp_dropdown_categories('taxonomy=post_tag&show_option_none=Select tag&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><div><input type="submit" value="View" /></div></noscript>
</div></form>

Sunday, July 22, 2012

How to execute shortcodes inside custom fields in wordpress

How to execute shortcodes inside custom fields in wordpress

WordPress by default do not allows shortcodes to be executed inside custom fields. If you want to display something with the help of shortcode inside a specific custom field, then you have to follow the simple steps

Put the following code into whatever page where you are displaying the results of the shortcode, and then change the my_wp_custom_field_here to the name of your own custom field.

<?php echo apply_filters('the_content', get_post_meta($post->ID, 'my_wp_custom_field_here', true)); ?>

Saturday, June 16, 2012

How to Remove the Width and Height Attributes From WP Image Uploader

How to Remove the Width and Height Attributes From WP Image Uploader


When we upload images in WordPress using image uploader and insert that image into wordpress posts, WordPress always automatically include the image width and height attributes in the html <img> tag. But sometimes that can give us some problem, because that may not fit in our layout . So using the following code we can get rid of those height and width attributes.

Just paste the following code into  functions.php file under theme folder.

add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}

Tuesday, June 5, 2012

How to display RSS feeds on WordPress

How to display RSS feeds on WordPress

If you ever want to display any rss feed on your WordPress blog then follow the process. The simple code that will do the job without using the deprecated wp_rss() function in wordpress.


The following code you have to paste in template pages wherever you wanna display the rss feed.
Please  update the feed url on line 3. and the Number of items you want to display can be defined on line 6.

<?php
include_once(ABSPATH . WPINC . '/rss.php');
$feed = 'http://sandipdas.in/feed';
$rss = fetch_feed($feed);
if (!is_wp_error( $rss ) ) :
    $maxitems = $rss->get_item_quantity(3);
    $rss_items = $rss->get_items(0, $maxitems);
    if ($rss_items):
        echo "<ul>\n";
        foreach ( $rss_items as $item ) :
            echo '<li>';
            echo '<a href="' . $item->get_permalink() . '">' . $item->get_title() . "</a>\n";
            echo '<p>' . $item->get_description() . "</li>\n";
        endforeach;
        echo "</ul>\n";
    endif;
endif;
?>

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off