We use the_post_thumbnails to get our images displayed.
We want to use them with a specific class called "test" but we cant manage it!
We use this code:
<?php the_post_thumbnail('pin-photo-large', array('class' => 'test')); ?>
But the output of the image seems like this:
class="test wp-post-image"
How can we overcome this issue and get an output like this ?
class="test"
Update: Add this to your functions.php file:
remove_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' );
Explanation: In media.php, the _wp_post_thumbnail_class_filter function adds the wp-post-image class to post thumbnails. By removing the begin_fetch_post_thumbnail_html action hook (located in default-filters.php), the function will no longer apply to the_post_thumbnail.
Old answer below:
I also searched for a proper way to filter out the wp-post-image class, alas to no avail. The only solution I figured out was to edit a (gasp!) core file: media.php and replace this:
function _wp_post_thumbnail_class_filter( $attr ) {
$attr['class'] .= ' wp-post-image';
return $attr;
}
by this:
function _wp_post_thumbnail_class_filter( $attr ) {
return $attr;
}
There's certainly a better way to do this than to patch a core file, but this can be a good temporary solution.
Add this to your functions.php file :
function remove_post_image_class($content) {
$post_string = $content;
$post_string = preg_replace('/<img class=".*?"/', '<img', $post_string);
return $post_string;
}
add_filter( 'the_content', 'remove_post_image_class' );
Enjoy
Related
I am working on a theme for a friend but get stuck ...
The comments and the comment form are inside a jquery tab.
To toggle the tab on a klick of the reply link i have to add #nav-comform to the link.
Example:
http://localhost/?p=109&replytocom=10#respond#nav-comform
I know i have to work with a filter in the functions.php but i have never done it before so i am a little lost and everything i try fail ...
I know it should be something like this filter example to add rel="nofollow" to the reply link:
function add_nofollow_to_reply_link( $link ) {
return str_replace( '")\'>', '")\' rel=\'nofollow\'>', $link );
}
add_filter( 'comment_reply_link', 'add_nofollow_to_reply_link' );
Maybe some one can lead me a way ?
Thank you very much !!
Try the following
function add_link_hash($args){
$args['respond_id'] = 'nav-comform';
return $args;
}
add_filter('comment_reply_link_args', 'add_link_hash', 10);
I want to customise the "recent posts" widget.
Since it's not good to edit the core for different reasons, I read about working it in functions.php extending WP_Widget with a new class and overriding the widget($args, $instance) function editing the code I want to customise, next to add it to the widget_init hook but I can't get how it works.
I mean, I think I should extend the WP_Widget_Recent_Posts then tell WP to use my class instead of the original one but....how can I do it?
Thanks
It looks like you can get away just by using the dynamic_sidebar_params() filter along with a search/replace.
add_filter( 'dynamic_sidebar_params', 'replace_widget_class' );
function replace_widget_class( $params ) {
if ( $params[0]['widget_name'] == 'Recent Posts' ) {
$params[0]['before_widget'] = str_replace( 'widget_recent_entries', 'widget_recent_entries_NEW', $params[0]['before_widget'] );
}
return $params;
}
I have migrated my contents from a Yii site to Wordpress and I have all my posts filled with div tags with all kind of styles and ids and class and I want to use a pattern to remove them all. I searched the internet and came with this function, yet it does remove the closing tag, but not the opening tags which contains other attributes like style id or class:
function remove_divs($data) {
$filteredContent = preg_replace(array('/<\s*div.*?>/', '</div>'), '', $data['post_content']);
$data['post_content'] = $filteredContent;
return $data;
}
add_filter('wp_insert_post_data', 'remove_divs', 99);
Can you help me figure what isn't working?
Being inspired by the previous answer, I edited my function and it worked just fine. Here is the code:
function remove_divs($data) {
$filteredContent = preg_replace(array('/<\s*div[^>]*>/', '</div>'), '', $data['post_content']);
$data['post_content'] = $filteredContent;
return $data;
}
add_filter('wp_insert_post_data', 'remove_divs', 99);
Thank you for your answer!
You can try this pattern
$pattern = "/<div[^>]*>(.*?)<\/div>/";
$content = '<div class="test">Alpha Beta Gamma</div>Test<div class="best">Delta Gamma Eta</div>';
preg_match_all($pattern, $content, $match);
var_dump($match);
Is it possible to remove the gallery shortcode from the content before when the_content() is executed? I searched codex and found remove_shortcode( $tag ) but they show no examples.
I tried adding to functions
function remove_gallery($content) {
$content .= remove_shortcode('[gallery]');
return $content;
}
add_filter( 'the_content', 'remove_gallery', 6);
It doesnt work..
Update:
I was able to unregister the shortcode using the code below, but it also removes the content
function remove_gallery($content) {
return remove_shortcode('gallery', $content);
}
add_filter( 'the_content', 'remove_gallery', 6);
I know this is a relative old question, but the strip_shortcodes function does work!
global $post;
echo strip_shortcodes($post->post_content);
Easiest way if you ask me..
Strange. remove_shortcode (codex link) doesn't take a second argument.
You're returning either the true or false return of the remove_shortcode function, not the content with the shortcode removed.
Try either something like this in that second version of your function up there:
remove_shortcode('gallery');
return $content;
Or just put
remove_shortcode('gallery');
In your functions.php file. The previous poster suggested including the [ ]'s, which I guess is wrong.
I think you should use sub string replacement like this:
function remove_gallery($content) {
return str_replace('[gallery]', '', $content);
}
add_filter( 'the_content', 'remove_gallery', 6);
Bear in mind, this method does not come with good performance.
update: You can unregister the shotcode in function.php by adding code:
remove_shortcode('[gallery]');
An old question, but after some digging and a combination of answers this worked for me:
<?php $content = get_the_content();
echo strip_shortcodes($content);?>
I was only inserting galleries, which I wanted to remove and display separately. Obviously if you want to remove a specific shortcode this might not be the solution for you.
following the tutorial at http://josephscott.org/archives/2010/03/database-powered-css-in-wordpress-themes/ i am trying to use wordpress' parse_request function to add some php-driven CSS... mostly style options set in my theme's options panel. i am aware that my code looks a little different from the author's, but i tried it his way already. i can add the
function kia_wp_head() {
wp_enqueue_style('dynamic', get_bloginfo('stylesheet_directory') . '/admin/ . '?my-custom-content=css');
}
add_action('wp_print_styles', 'kia_wp_head');
//this shows up properly enqueued but when i click on it in source it just brings up a directory listing for the admin folder
function my_custom_wp_request( $wp ) {
if( isset($_GET['my-custom-content']) && $_GET['my-custom-content'] == 'css' ) {
# get theme options
header( 'Content-Type: text/css' ); ?>
body {
background-color: <?php echo 'red'; ?>
}
<?php
exit;
}
}
add_action( 'parse_request', 'my_custom_wp_request' );
but since the background never turns red, I am either not implementing this properly or the tutorial is missing a critical step. i've also tried the other method of putting the dynamic css in its own custom-css.php file, which is my ultimate goale, but i was just trying to see if i could interact with the parse request function properly:
function my_custom_wp_request( $wp ) {
if (
isset($_GET['my-custom-content'])
&& $_GET['my-custom-content'] == 'css'
) {
# get theme options
header( 'Content-Type: text/css' );
require dirname( __FILE__ ) . '/custom-css.php';
exit;
}
}
add_action( 'parse_request', 'my_custom_wp_request' );
here i'm not sure what dirname( FILE ) means exactly, but i have also tried using a hardcoded path and that didn't work either.
so how do i get parse_request to see my php-driven stylesheet?
/* EDIT FOR SOLUTION */
basically this doesn't work w/ wp_enqueue_style
wp_enqueue_style('dynamic', get_bloginfo('stylesheet_directory') . '/admin/ . '?my-custom-content=css');
but DOES work as described at josephscott.org by inserting the style tag directly into the head
. '/admin?my-custom-content=css">
i found that it doesn’t work w/ wp_enqueue_script. it does work as written in the tutorial by manually adding the script tag to the header.