Google help on software/programming And Windows Tricks

helponsoftware

Friday, 28 August 2015

image gallery restriction for any user in wordpress

function ml_restrict_media_library( $wp_query_obj ) {
    global $current_user, $pagenow;
    if( !is_a( $current_user, 'WP_User') )
        return;
    if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
        return;
    if( !current_user_can('update_core') )
        //$wp_query_obj->set('author', $current_user->ID );
             $wp_query_obj->set('post__in',kk_ids());
    return;
}
function kk_ids(){
// $posts =query_posts(array('post_type'=>'kklibrary','posts_per_page'=>-1));
// foreach ($posts as $post):
// $post_thumbnail_id[] = get_post_thumbnail_id( $post->ID);
// endforeach;
// wp_reset_query();
return $post_thumbnail_id= array(248,249,250,251,252,253,254);
}
add_action('pre_get_posts','ml_restrict_media_library');

hide plugin from listing in wordpress

function mu_hide_plugins_network( $plugins ) {
    // let's hide akismet
    if( in_array( 'front-end-publishing/fepublishing.php', array_keys( $plugins ) ) ) {
        unset( $plugins['front-end-publishing/fepublishing.php'] );
    }
    return $plugins;
}

add_filter( 'all_plugins', 'mu_hide_plugins_network' );

Update the Image Uploaded in frontend or Regenerate Thumbnails

$fullsizepath = get_attached_file( $attach_id );
$metadata = wp_generate_attachment_metadata( $attach_id, $fullsizepath );
wp_update_attachment_metadata( $attach_id,  $metadata );

if ( is_wp_error( $metadata ) )
echo 'Images not upoaded Correctly Please try Again.';
if ( empty( $metadata ) )
echo 'Unknown failure reason.';

Add Any Charge in Checkout Woocommerce

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 0.01;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;   
    $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );

}

Remove Duplicate Entry in Contact Form 7

add_filter( 'wpcf7_validate', 'email_already_in_db', 10, 2 );
function email_already_in_db ( $result, $tags ) {
// retrieve the posted email
$form = WPCF7_Submission::get_instance();
$email = $form->get_posted_data('email');
// if already in database, invalidate
if( is_already_submitted('Contact form 1' ,'email',$email ) )
$result->invalidate('email', 'Your email exists in our database');
// return the filtered value
return $result;
}
function is_already_submitted($formName, $fieldName, $fieldValue){
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$exp = new CFDBFormIterator();
$atts = array();
$atts['show'] = $fieldName;
$atts['filter'] = "$fieldName=$fieldValue";
$exp->export($formName, $atts);
$found = false;
while ($row = $exp->nextRow()) {
$found = true;
}
return $found;
}
Just set the field name, and form name in above function.