Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

help with custom php for thematic framework theme WordPress

  • SOLVED

I'm working on a thematic child theme and need some help with some custom coding in the functions.php file.

1. for submitted posts, replace the post title on index, category, archive and single post pages with the first linked text from the post area.

2. within the post area, display a linked screenshot of the webpage linked from the first linked text in the post area by tying into the Wordpress core as described here: http://www.binarymoon.co.uk/projects/bm-shots-automated-screenshots-website/

so if there's a link like this <a href="http://www.bbc.co.uk">BBC website is here</a> then the post title becomes "BBC website is here" and the post contains a screenshot image of the website www.bbc.co.uk.

Ideally I'd like to achieve all this using hooks / filters... anyone able to help?

Answers (3)

2010-02-21

Cristian Antohe answers:

Hi,

This is a different solution then Michael's. For this you don't need to have the plugin activated and it searches for the first link differently.

You can see it working here:
http://www.cozmoslabs.com/projects/nomadic/



// Search for first link function
function search_first_link($content){
$more = 1;
$count = substr_count($content, '<a');
$start = 0;
for($i=1;$i<=$count;$i++) {
$linkBeg = strpos($content, '<a', $start);
$post = substr($content, $linkBeg);
$linkEnd = strpos($post, '</a>');
$postOutput = substr($post, 0, $linkEnd+4);
$link[$i] = $postOutput;
$start=$linkEnd+1;
}
$more = 0;
if (!empty($link[1]))
{
return $link[1];
} else { return false; }
}

function extract_url_from_link($link){
$more = 1;
$count = substr_count($link, 'http://');
$start = 0;
for($i=1;$i<=$count;$i++) {
$urlBeg = strpos($link, 'http://', $start);
$post = substr($link, $urlBeg);
$urlEnd = strpos($post, '"');
$postOutput = substr($post, 0, $urlEnd);
$url[$i] = $postOutput;
$start=$linkEnd+1;
}
$more = 0;
if (!empty($url[1]))
{
return $url[1];
} else { return false; }
}

// Create the page title
function custom_title($the_title){
global $post;
$content = get_the_content();

$post_link = search_first_link($content);
if ($post_link)
{
return '<h2 class="entry-title">' . $post_link . '</h2>';
} else { return $the_title; }


}
add_filter('thematic_postheader_posttitle' ,'custom_title');

// Add the automatic image - With help from Ben Gillbanks - http://www.binarymoon.co.uk/
function bm_mshot ($url = '', $width = 250) {
if ($url != '') {
return 'http://s.wordpress.com/mshots/v1/' . urlencode(clean_url($url)) . '?w=' . $width;
} else {
return '';
}
}

function add_image_url($content){

$post_link = search_first_link($content);
$url = extract_url_from_link($post_link);
$imageUrl = bm_mshot ($url, 250);

if ($imageUrl == '') {
return $content;
} else {
$image = '<img src="' . $imageUrl . '" alt="' . $url . '" width="' . $width . '" />';
return '<div class="browsershot mshot"><a href="' . $url . '">' . $image . '</a></div>' . $content;
}
}

add_filter('the_content', 'add_image_url');


Dave Smith comments:

code has broken the theme... have cut and pasted within the opening and closing php bits... is it me or you?


Cristian Antohe comments:

The code works on my development site. Try and delete the closing php tag ?>

See if that works!


Cristian Antohe comments:

Also disable the plugin from Binary Moon. The bm_mshot function will be declared twice and that probably brakes your theme.


Dave Smith comments:

can you post a link to a completed functions.php file for me so i can just grab it please?


Cristian Antohe comments:

Check this out: http://www.cozmoslabs.com/projects/nomadic/wp-content/themes/nomadicmatt/functions.txt

I've also changed the function name.


Dave Smith comments:

scrub that it's working. well done christian. nice work. for a $20 bonus, is there a way of removing the original linked text from within the post? So all I have is the linked title, the screenshot, and the remaining copy without the linked text?


Cristian Antohe comments:

I've updated the functions.txt link with the code that removes your url from the content!

You can check it out here: http://www.cozmoslabs.com/projects/nomadic/ (the link isn't showing anymore.)

2010-02-21

Erez S answers:

you should use the filter the_title,and in order to find the first link you can use the function get_the_content and to get the link with regex webmasterworld.com/php/3698684.htm and for question use regex again and get the img url and simply add html tags of link and image to the content with the filter the_content. enjoy


Erez S comments:

more info about regex stackoverflow.com/questions/1399768/php-regex-or-alt-method-for-anchor-tags and the function to get the screenshot img is m_mshot ( $url , $width ); and simply wrap it with anchor tag

2010-02-21

Michael Fields answers:

Here's the solution that I came up with. all code goes in functions.php of your child theme.

add_filter( 'thematic_post', 'mfields_append_image_to_thematic_content' );
if( !function_exists( 'mfields_append_image_to_thematic_content' ) ) {
function mfields_append_image_to_thematic_content( $c ) {
$first_link = array();
if( function_exists( 'bm_mshot' ) )
$first_link = mfields_get_first_link_in_post_content( $c );
if( isset( $first_link['url'] ) )
$c = do_shortcode( '[browsershot url="' . $first_link['url'] . '" width="' . 250 . '"]' );
return $c;
}
}

if( !function_exists( 'mfields_get_first_link_in_post_content' ) ) {
function mfields_get_first_link_in_post_content( $content ) {
$o = array();
$has_link = preg_match( '#<a(.*)>(.*)</a>#', $content, $anchor );
if( $has_link !== 0 && $has_link !== false ) {
list( $tag, $attr, $text ) = $anchor;
$o['anchor'] = $tag;
$o['text'] = $tag;
if( !empty( $attr ) ) {
$has_href = preg_match( '#href="(.*)?"#', $attr, $href );
if( $has_href !== 0 && $has_href !== false ) {
list( $href_attr, $url ) = $href;
$o['url'] = $url;
}
}
}
return $o;
}
}

add_filter( 'thematic_postheader_posttitle', 'mfields_link_title' );
if( !function_exists( 'mfields_link_title' ) ) {
function mfields_link_title( $c ) {
global $post;
$first_link = mfields_get_first_link_in_post_content( $post->post_content );
if( !empty( $first_link['anchor'] ) ) {
if ( is_singular() )
return '<h1 class="entry-title">' . $first_link['anchor'] . "</h1>\n";
else
return '<h2 class="entry-title">' . $first_link['anchor'] . "</h2>\n";
}
return $c;
}
}


Dave Smith comments:

Hey Michael,

just put this into functions.php and whilst the title bit is working, the image is not.. the post is still just showing the linked text. See demo here: http://nutritionuk.net/

do i need to have the bm shots plugin installed for your code to work... ideally i'd like the solution to work without the plugin..

kind regards,

Dave


Dave Smith comments:

ok just installed and activated the plugin and i see it's working... is it much more effort to get it to work independently of the plugin Michael?

sorry if i wasn't clear at the outset.