• Resolved beniEllis

    (@beniellis)


    I have a snippet for customizing my rss feed. It works fine when it is in my functions.php file, but breaks my website — the white screen of “oh s**t!” — when I add it via the Code Snippet plugin. Snippets are a bit beyond my comfort zone, so I don’t understand why they work in the functions file, but not via the plugin.

    Any thoughts how to make this work? This is the code I used to create the snippet:

    // add custom fields to rss feed
    
        function add_feed_content($content) {
        global $wp_query;
        $postid = $wp_query->post->ID;
    
        $city = get_post_meta($postid, 'property_city', true);
        if(is_feed()) {
        if($city !== '') {
        $content = $content."<div><h3>".$city." in ""</h3></div>
        ";
        }
        else {
        $content = $content;
        }
        }
    
        $area = get_post_meta($postid, 'property_area', true);
        if(is_feed()) {
        if($area !== '') {
        $content = $content."<div><h3>".$area."</h3></div>
        ";
        }
        else {
        $content = $content;
        }
        }
    
        return $content;
        }
        add_filter('the_excerpt_rss', 'add_feed_content');
        add_filter('the_content', 'add_feed_content');
    
    // edit excerpt to 45 words
        function custom_excerpt_length( $length ) {
    	return 45;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
    
    // add featured image to rss feed
    function featuredtoRSS($content) {
    global $post;
    if ( has_post_thumbnail( $post->ID ) ){
    $content = '' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:left; margin:0 15px 15px 0;' ) ) . '' . $content;
    }
    return $content;
    }
    
    add_filter('the_excerpt_rss', 'featuredtoRSS');
    add_filter('the_content_feed', 'featuredtoRSS');
    
    // ----- end rss custom fields ------------------

    https://wordpress.org/plugins/code-snippets/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi
    (Sorry i’m french and my english is very bad) 🙂

    I have the same “issue” with a (maybe too complex ?) code that use with WC Vendors plugin (for remove all” Sold by)

    // Put this in your themes functions.php
    // Remove all existing actions and filters
    remove_action( 'woocommerce_after_shop_loop_item', 	array( 'WCV_Vendor_Shop', 'template_loop_sold_by'), 9, 2);
    remove_filter( 'woocommerce_get_item_data', 		array( 'WCV_Vendor_Cart', 'sold_by' ), 10, 2 );
    remove_action( 'woocommerce_product_meta_start', 	array( 'WCV_Vendor_Cart', 'sold_by_meta' ), 10, 2 );
    remove_filter( 'woocommerce_order_product_title', 	array( 'WCV_Emails', 'show_vendor_in_email' ), 10, 2 );
    remove_action( 'woocommerce_add_order_item_meta', 	array( 'WCV_Vendor_Shop', 'add_vendor_to_order_item_meta'), 10, 2 );
    remove_action( 'woocommerce_after_shop_loop_item',  array( $wcvendors_pro->wcvendors_pro_store_controller, 'loop_sold_by' ), 8  );
    remove_action( 'woocommerce_product_meta_start',  	array( $wcvendors_pro->wcvendors_pro_store_controller, 'product_sold_by' ), 8 );
    remove_filter( 'woocommerce_get_item_data',  		array( $wcvendors_pro->wcvendors_pro_store_controller, 'cart_sold_by' ), 8, 2 );
    // Hook into the product loop
    add_action( 'woocommerce_after_shop_loop_item', 'custom_loop_sold_by', 9 );
    function custom_loop_sold_by($product_id) {
    
    	$vendor_id = WCV_Vendors::get_vendor_from_product( $product_id );
    
    	if ( WCV_Vendors::is_vendor( $vendor_id) ) { 
    
    		$shop_url 	=  '';
    		$store_id 	= WCVendors_Pro_Vendor_Controller::get_vendor_store_id( $vendor_id );
    		$sold_by 		= apply_filters( 'wcv_loop_sold_by',
    			array(
    				'product_id'		=> $product_id,
    				'vendor_id'			=> $vendor_id,
    				'shop_url' 			=> get_the_permalink( $store_id ),
    				'shop_name'			=> WCV_Vendors::get_vendor_sold_by( $vendor_id ),
    				'wrapper_start'		=> '<small class="wcv_sold_by_in_loop">',
    				'wrapper_end'		=> '</small><br />',
    				'title'				=> __( 'Sold by: ', 'wcvendors-pro' )
    			)
    		);
    		echo $sold_by[ 'wrapper_start' ];
    		echo $sold_by[ 'title' ] .'<a href="'.$sold_by[ 'shop_url' ].'">'. $sold_by[ 'shop_name' ] .'</a>';
    		echo $sold_by[ 'wrapper_end' ];
    	}
    } //custom_loop_sold_by()
    // Hook into the cart data
    add_filter( 'woocommerce_get_item_data', 'custom_cart_sold_by', 10, 2 );
    function custom_cart_sold_by( $values, $cart_item ) {
    	$vendor_id = $cart_item[ 'data' ]->post->post_author;
    	if ( WCV_Vendors::is_vendor( $vendor_id ) ) {
    		$is_vendor 	= WCV_Vendors::is_vendor( $vendor_id );
    		$store_id 	= $is_vendor ? WCVendors_Pro_Vendor_Controller::get_vendor_store_id( $vendor_id ) : 0;
    		$shop_url 	= ( $store_id 	!= 0 ) ? get_the_permalink( $store_id ) : '';
    		$sold_by 	= '';
    		if ( $is_vendor ) {
    			$sold_by = sprintf( '<a href="%s" target="_TOP">%s</a>', $shop_url, WCV_Vendors::get_vendor_sold_by( $vendor_id ) );
    		} else {
    			$sold_by =  get_bloginfo( 'name' );
    		}
    
    		$values[] = array(
    			'name' => apply_filters( 'wcv_cart_sold_by', __( 'Sold by', 'wcvendors-pro' ) ),
    			'display' => $sold_by,
    		);
    		return $values;
    	}
    	return $values;
    } // custom_cart_sold_by()
    // Hook into the single product meta template
    add_action( 'woocommerce_product_meta_start', 'custom_product_sold_by_meta', 10, 2 );
    function custom_product_sold_by_meta() {
    	$vendor_id = get_the_author_meta( 'ID' );
    
    	if ( WCV_Vendors::is_vendor( $vendor_id ) ) {
    		$shop_url 	=  '';
    		$store_id 	= WCVendors_Pro_Vendor_Controller::get_vendor_store_id( $vendor_id );
    		$sold_by 		=  array(
    				'vendor_id'			=> $vendor_id,
    				'shop_url' 			=> get_the_permalink( $store_id ),
    				'shop_name'			=> WCV_Vendors::get_vendor_sold_by( $vendor_id ),
    				'wrapper_start'		=> '<small class="wcv_sold_by_in_loop">',
    				'wrapper_end'		=> '</small><br />',
    				'title'				=> __( 'Sold by: ', 'wcvendors-pro' )
    		);
    		echo $sold_by[ 'wrapper_start' ];
    		echo $sold_by[ 'title' ] .'<a href="'.$sold_by[ 'shop_url' ].'">'. $sold_by[ 'shop_name' ] .'</a>';
    		echo $sold_by[ 'wrapper_end' ];
    	}
    } // custom_product_sold_by_meta()

    Best regards,

    🙂

    Plugin Author Shea Bunge

    (@bungeshea)

    beniEllis:

    Your snippet fails to save because there is an error. On like 10, you have an extra "" double quote mark. Remove this so the line 10 looks like this:

    $content = $content."<div><h3>".$city." in </h3></div>

    Plugin Author Shea Bunge

    (@bungeshea)

    MoiMM:

    Your problem is actually a bit different. It’s because remove_action() calls don’t always work when places directly before snippets, as snippets run before functions.php code. Try using this code instead:

    // Remove all existing actions and filters
    add_action( 'init', function () {
    	remove_action( 'woocommerce_after_shop_loop_item', 	array( 'WCV_Vendor_Shop', 'template_loop_sold_by'), 9, 2);
    	remove_filter( 'woocommerce_get_item_data', 		array( 'WCV_Vendor_Cart', 'sold_by' ), 10, 2 );
    	remove_action( 'woocommerce_product_meta_start', 	array( 'WCV_Vendor_Cart', 'sold_by_meta' ), 10, 2 );
    	remove_filter( 'woocommerce_order_product_title', 	array( 'WCV_Emails', 'show_vendor_in_email' ), 10, 2 );
    	remove_action( 'woocommerce_add_order_item_meta', 	array( 'WCV_Vendor_Shop', 'add_vendor_to_order_item_meta'), 10, 2 );
    	remove_action( 'woocommerce_after_shop_loop_item',  array( $wcvendors_pro->wcvendors_pro_store_controller, 'loop_sold_by' ), 8  );
    	remove_action( 'woocommerce_product_meta_start',  	array( $wcvendors_pro->wcvendors_pro_store_controller, 'product_sold_by' ), 8 );
    	remove_filter( 'woocommerce_get_item_data',  		array( $wcvendors_pro->wcvendors_pro_store_controller, 'cart_sold_by' ), 8, 2 );
    } );
    
    // Hook into the product loop

    The rest of the code underneath `
    // Hook into the product loop` is the same.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Snippet works in functions.php but not in Snippets plugin’ is closed to new replies.