Support » Plugin: Content Blocks (Custom Post Widget) » SOLUTION – JetPack Sharing incompatibility

  • I’ve experienced the same problem explained in an old support topic JetPack Sharing oncompatibility.

    The issue is:

    1. content_block types are private (to avoid showing up on search results and and prevent SEO issues).

    2. Since content_block is private JetPack filters out these posts when showing the sharing meta box and when saving its value.

    3. Custom Post Widget plugin doesn’t set global $post to content_block post before calling ‘the_content’ filter hook, which is used by function sharing_display() in …/plugins/jetpack/modules/sharedaddy/sharing-service.php.

    One solution is:

    1. Not sure if this is the preferred WordPress way of doing this, but modify the …/plugins/custom-post-widget/post-widget.php code line +102 to set global $post accurately. I don’t like modifying plugins, so please share if there is another way to do this inside the theme’s functions.php file. Or, if adequate, I suggest Johan includes it in the Custom Post Widget plugin code.

    if ( !$apply_content_filters ) { // Don't apply the content filter if checkbox selected
            global $post;
            $post = get_post( $custom_post_id, OBJECT );
            setup_postdata( $post );
    	$content = apply_filters( 'the_content', $content);
            wp_reset_postdata();
        }

    2. Add these two functions to your theme’s functions.php file

    add_action( 'add_meta_boxes', 'mytheme_add_meta_boxes'  );
        add_action( 'save_post', 'mytheme_save_post') );
    
        // Add Meta Box to Custom Post Widget Content
        // JETPACK: +66 sharedaddy.php - function sharing_add_meta_box()
        function mytheme_add_meta_boxes() {
            global $post;
            if ( empty( $post ) ) {
                return;
            }
    
            $post_types = get_post_types( array( 'public' => false ) );
    
            // Post Type Exception
            $post_type = 'content_block';
    
            if ( in_array( $post_type, $post_types ) ) {
                $title = apply_filters( 'sharing_meta_box_title', __( 'Sharing', 'jetpack' ) );
                if ( $post->ID !== get_option( 'page_for_posts' ) ) {
                    add_meta_box( 'sharing_meta', $title, 'sharing_meta_box_content', $post_type, 'advanced', 'high' );
                }
            }
    
        }
    
        // Save Sharing Meta Box Value
        // JETPACK: +139 sharedaddy.php - function sharing_meta_box_save()
        function mytheme_save_post( $post_id ) {
    	    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    	        return $post_id;
    
            // Record sharing disable
            $post_type_object = get_post_type_object( $_POST['post_type'] );
    
            // Post Type Exception
            $post_type = 'content_block';
    
            if ( isset( $_POST['post_type'] ) && ( $post_type_object = get_post_type_object( $_POST['post_type'] ) ) && ( $post_type_object->name == $post_type ) ) {
                if ( current_user_can( 'edit_post', $post_id ) ) {
                    if ( isset( $_POST['sharing_status_hidden'] ) ) {
                        if ( !isset( $_POST['enable_post_sharing'] ) ) {
                            update_post_meta( $post_id, 'sharing_disabled', 1 );
                        } else {
                            delete_post_meta( $post_id, 'sharing_disabled' );
                        }
                    }
                }
            }
    
            return $post_id;
    
        }

    https://wordpress.org/plugins/custom-post-widget/

  • The topic ‘SOLUTION – JetPack Sharing incompatibility’ is closed to new replies.