Support » Plugin: Custom Field Suite » [Plugin: Custom Field Suite] $cfs usage inside the_content filter return fatal error

  • Resolved Miguel Peixe

    (@miguelpeixe)


    Hi,

    I’m trying to use $cfs inside a the_content filter, but I get “PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 125 bytes)”.

    Here’s a code example:

    add_filter('the_content', 'custom_content');
    
    function custom_content($content) {
    	global $post;
    	global $cfs;
    	$fields = $cfs->get();
    
    	return $content . $fields['author'];
    }

    Any ideas?

    http://wordpress.org/extend/plugins/custom-field-suite/

Viewing 15 replies - 1 through 15 (of 25 total)
  • Plugin Author logikal16

    (@logikal16)

    Miguel,

    Your server has reached its memory limit. You’ll need to either increase PHP’s memory limit (higher than 128MB), or try deactivating some unnecessary plugins.

    You also could try adding the following into wp-config.php:

    ini_set('memory_limit', '192M');

    Hi logikal16,

    Actually I don’t think that’s the issue, using $cfs->get(); on the same post outside the content filter works fine and my memory limit is already set to a higher limit.

    Is it possible that there’s a bug on $cfs usage inside a WordPress filter?

    Even trying to get a single field, like $cfs->get(‘author’), I get the same result…

    Plugin Author logikal16

    (@logikal16)

    Is it possible that there’s a bug on $cfs usage inside a WordPress filter?

    Unlikely. It doesn’t matter where $cfs is being called — as long as it gets passed the correct post ID.

    Try commenting out the CFS stuff, and then var_dump($post). Does the correct post get outputted?

    var_dump($post) outputs correctly.

    It’s really strange to me that $cfs->get(); works on single.php template but not inside the_content filter, why would that happen?

    Plugin Author logikal16

    (@logikal16)

    Ditto, I’ve never heard of that before.

    What happens if you try manually passing the post id into $cfs, e.g.

    $fields = $cfs->get(false, $post->ID);

    Same problem

    Plugin Author logikal16

    (@logikal16)

    Are you using a CFS wysiwyg field?

    I bet my breakfast he is

    Plugin Author logikal16

    (@logikal16)

    Thanks to @sc0ttkclark, who pointed out that the WYSIWYG field also uses the_content(), and this leads to infinite recursion. This isn’t necessarily a bug, but it does mean that if you want to continue using the_content(), you’ll need to modify your function:

    function custom_content($content) {
        global $cfs, $post;
    
        // Only call $cfs->get() once (prevent recursion)
        if (!isset($cfs->the_content_loaded)) {
            $fields = $cfs->get();
            $cfs->the_content_loaded = true;
            return $content . $fields['author'];
        }
        else {
            return $content;
        }
    }

    Got the issue, but it’s still not working.
    It’s not preventing the recursion here.

    Can I suggest using wpautop function instead of applying the_content filters to the wysiwyg field? Or making it optional.

    Seems to me that it’s a mix of a WordPres and a CFS bug. I think people do put their custom fields data using the_content filter and it should be able to work fine…

    If it helps the discussion, someone also had this problem with the Advanced Custom Fields plugin
    http://support.advancedcustomfields.com/discussion/2245/applying-the_content-filters-on-wysiwyg-fields-should-be-optional/p1

    Thanks a lot for the support. I’m temporarily changing the wysiwyg field code directly on the plugin, til I get a better solution for it. If I put the custom fields directly on the template it’ll mess up my theme structure plans :/

    @logikal16 – Try placing that same kind of logic in your field itself, put store the variable as static in the field type’s class, so it’s localized to the needs of that field.

    Plugin Author logikal16

    (@logikal16)

    WordPress uses a lot more than wpautop to sanitize the_content:

    add_filter( 'the_content', 'wptexturize'        );
    add_filter( 'the_content', 'convert_smilies'    );
    add_filter( 'the_content', 'convert_chars'      );
    add_filter( 'the_content', 'wpautop'            );
    add_filter( 'the_content', 'shortcode_unautop'  );
    add_filter( 'the_content', 'prepend_attachment' );

    Other plugins add additional WYSIWYG buttons / features, which would break it we replaced the_content() on the WYSIWYG field.

    I think people do put their custom fields data using the_content filter and it should be able to work fine…

    I strongly advise against appending your custom fields into a hook (whether the_content or something else). Adding it separately ensures that your CFs are safe from other plugins.

    What about my suggestion, I think there could be away to put this ‘the_content’ check internally within the field itself to check if it’s going to recurse or not and whether to apply the filters or to pass the value straight through?

    I know what you mean but it creates lots of other problems for me. By concept the_content filter should be able to increment data. WordPress should have another filter for that, or a different filter to work with non-direct-content formatting. Agree?

    I’m still going to work with wpautop for now, I’ll never use shortcodes, attachments, smiles, or anything like that in my wysiwyg…

    Thanks a lot, though 🙂

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘[Plugin: Custom Field Suite] $cfs usage inside the_content filter return fatal error’ is closed to new replies.