Support

Account

Home Forums General Issues Custom Scripts

Solving

Custom Scripts

  • Im trying to create an editable section for an admin to input custom scripts to particular pages such as google analytics. With both textareas and WYSIWYG editors, it is either adding in <p> tags or is changing my <script> tags to <script>.

    After doing some research it seems as though the plugin used to have functionality to stop this but it no longer seems to be there. Is there a way around this?

  • Hi @mdurchholz

    For the text area field, you should be able to set the New Lines option (PRO version) or Formatting option (Free version) to “No Formatting”.

    For the WYSIWYG field, you should be able to remove the <p> tag like this:

    function my_acf_add_local_field_groups() {
        remove_filter('acf_the_content', 'wpautop' );
    }
    add_action('acf/init', 'my_acf_add_local_field_groups');

    I hope this helps 🙂

  • Thanks for the tip. I’ll check it out.

  • Wouldn’t that script stop all wysiwyg editors? I only want it for one area. Is there a way to modify it to target one specific field group?

    Also, is there a way to stop the textarea fields from changing my <> characters into HTML entities? I’d prefer to use the textarea rather than adjusting the WYSIWYG editor.

  • Hi @mdurchholz

    Yes, it would affect all WYSIWYG editor. If you want to do it only for certain field, you can use the acf/load_value filter instead. It should be like this:

    function my_acf_load_value( $value, $post_id, $field ) {
        
        remove_filter('acf_the_content', 'wpautop' );
    
        return $value;
    }
    
    // acf/load_value/name={$field_name} - filter for a specific value load based on it's field name
    add_filter('acf/load_value/name=wysiwyg_editor', 'my_acf_load_value', 10, 3);

    I believe the text area field won’t convert special chars like <>. I’ve just tested it on my installation and it executed the script correctly. Maybe it has something to do with your theme, plugins or database encodings.

    To check if it’s your theme or plugins fault, could you please try to reproduce the issue on one of the WordPress’ stock themes (like Twenty Sixteen) with other plugins deactivated? If it disappears, then you can activate the theme and plugins one by one to see which one causes the issue.

    To check if it’s your database encodings fault, you need to get in touch with your hosting provider.

    I hope this makes sense 🙂

  • This is a really perfect for a new blogger like me who doesn’t want their site to be messy with those spammers who don’t even read your post but they have the guts to comment in your site. Thanks again.
    Speaking of site, take a look at here: chokdeebacarrat

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Custom Scripts’ is closed to new replies.