Skip to content

Instantly share code, notes, and snippets.

@rayflores
Last active November 19, 2015 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rayflores/086e05ed4e0e1a0e8fc9 to your computer and use it in GitHub Desktop.
Save rayflores/086e05ed4e0e1a0e8fc9 to your computer and use it in GitHub Desktop.
add attachment meta, and remove pinterest pinit button from those images with meta box checked...
<?php
/**
* Plugin Name: Pinit Only On Selected Media
* Plugin URI: http://www.rayflores.com/plugins/pinit-onlyon/
* Version: 1.0
* Author: Ray Flores
* Author URI: http://www.rayflores.com
* Description: Adds ability to select media attachments to be unpinned.
* Requires at least: 4.0
* Tested up to: 4.1
* screenshot of metabox: http://screencast.com/t/4o12zuTB
* screenshot of image with no pinit button: http://screencast.com/t/GvDre4Beh
*/
// first lets add a meta_box to the attachment area
function attachment_fields_to_edit_pinit_only_image( $form_fields, $post ) {
$checked = get_post_meta( $post->ID, 'pinitonly', false ) ? 'checked="checked"' : '';
$form_fields['pinitonly'] = array(
'label' => 'Remove Pin it ?',
'input' => 'html',
'html' => "<input type=\"checkbox\"
name=\"attachments[{$post->ID}][pinitonly]\"
id=\"attachments[{$post->ID}][pinitonly]\"
value=\"1\" {$checked}/>",
'helps' => 'Do you want the Pinit Pro button to appear on this image?'
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'attachment_fields_to_edit_pinit_only_image', null, 2 );
// then let's save it to the db
function attachment_fields_to_save_pinit_only_image($post, $attachment) {
if(isset($attachment['pinitonly']))
{
update_post_meta($post['ID'], 'pinitonly', 1);
} else {
update_post_meta($post['ID'], 'pinitonly', 0);
}
return $post;
}
add_filter( 'attachment_fields_to_save', 'attachment_fields_to_save_pinit_only_image', null, 2 );
//add_action('wp_footer','remove_pinit_from_image',100);
add_filter( 'the_content', 'remove_pinit_from_image', 100 );
function remove_pinit_from_image($content){
global $pib_options, $post;
$postID = $post->ID;
if ( is_page() || is_singular() )
{
$imgPattern = "/<img([^\>]*?)>/i";
if ( preg_match_all( $imgPattern, $content, $imgTags )) {
foreach ( $imgTags[0] as $imgTag ) {
preg_match( '@src="([^"]+)"@' , $imgTag, $match );
$image_src = $match[1];
// will return /images/image.jpg
//echo $image_src;
$imgID = get_attachment_id_from_src ($image_src);
$img_meta = get_post_meta($imgID,'pinitonly',true);
if ($img_meta != 0){
// you found the media items with "remove pin it option" !!
//Skip adding hover effect if image contains certain classes
$skipTheseClasses = 'pib-hover-img,pin-it-btn-custom-img,pib-nohover';
//Add user-added CSS classes to skip hovering on
if ( ! empty( $pib_options['hover_btn_ignore_classes'] ) ) {
$skipTheseClasses .= ',' . $pib_options['hover_btn_ignore_classes'];
}
//Convert comma delimiters to pipes for now to work with current regex
$skipTheseClasses = str_replace( ',', '|', $skipTheseClasses );
if ( ! preg_match( '/' . $skipTheseClasses . '/i', $imgTag ) ) {
// The ignored classes shouldn't have pib-hover-img class added to them.
if ( ! preg_match( '/class=/i', $imgTag ) ) {
//image is good to go -- add the hover class
$pattern = $imgPattern;
$replacement = '<img class="pib-nohover" $1>';
}
else {
//image matches an class to skip, so don't add hover
$pattern = "/<img(.*?)class=('|\")([A-Za-z0-9 \/_\.\~\:-]*?)('|\")([^\>]*?)>/i";
$replacement = '<img$1class=$2$3 pib-nohover$4$5>';
}
$replacedImgTag = preg_replace( $pattern, $replacement, $imgTag );
$content = str_replace( $imgTag, $replacedImgTag, $content );
} // end if ( ! preg_match( '/' . $skipTheseClasses . '/i', $imgTag )
} // end if $img_meta === 1
} // end foreach
} // end if ( preg_match_all( $imgPattern, $content, $imgTags ))
return $content;
} // end if is_page()
} // end function
function get_attachment_id_from_src ($image_src) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$id = $wpdb->get_var($query);
return $id;
}
@greguly
Copy link

greguly commented Nov 19, 2015

Cool, just needs a bit of formatting ;-)

@greguly
Copy link

greguly commented Nov 19, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment