WordPress.org

Ready to get started?Download WordPress

Forums

Email Address Encoder
[resolved] Interferes with PHP preg_replace in My Function (8 posts)

  1. Adam
    Member
    Posted 1 year ago #

    Hi @Till

    I recently added a new function to my site which filters content looking for text email addresses. When it finds an email address, it converts the email address to a "maito" link.

    What happens when I have the Email Address Encoder plugin enabled is that the function does not convert the emails to "mailto" links. It leaves the email addresses as just text. However, I'd like to use the EAE plugin for its security features.

    Here is the function I'm using:

    function txt_filter($string) {
    	$search = array('/([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/');
    	$replace = array('<a href="mailto:$1>$1</a>');
    	$processed_string = preg_replace($search, $replace, $string);
    	echo $processed_string;
    }

    http://wordpress.org/extend/plugins/email-address-encoder/

  2. Till Krüss
    Member
    Plugin Author

    Posted 1 year ago #

    Your regexp won't work with already encoded email addresses, so it needs to be called before the EAE plugin, which would be using the the_content filter with a priority of 999 or less.

  3. Adam
    Member
    Posted 1 year ago #

    Thanks for your response! How do I set the priority for my function?

  4. Till Krüss
    Member
    Plugin Author

    Posted 1 year ago #

  5. Adam
    Member
    Posted 1 year ago #

    Would this be correct?

    function txt_filter($string) {
    	$search = array('/([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/');
    	$replace = array('<a href="mailto:$1>$1</a>');
    	$processed_string = preg_replace($search, $replace, $string);
    	echo $processed_string;
    }
    add_filter( '', 'txt_filter', 10, '' );

    I have a feeling it's wrong. This feels somewhat "over my head".

  6. Till Krüss
    Member
    Plugin Author

    Posted 1 year ago #

    add_filter( 'the_content', 'txt_filter', 10 );

    Make sure you don't call txt_filter anywhere else manually.

  7. Adam
    Member
    Posted 1 year ago #

    My function is used as follows:

    <?php txt_filter( get_the_content() ); ?>

    and

    "Advanced Custom Fields" plugin field:
    <?php txt_filter( get_field('field_name') ); ?>

    Your plugin does filter any WYSIWYG / WP Editor fields created with the plugin, Advanced Custom Fields.

    The code you supplied above may not work for me. :(

    I might just have to use a JS solution.

  8. Till Krüss
    Member
    Plugin Author

    Posted 1 year ago #

    Quick and dirty. Try this:
    $content = apply_filters( 'the_content', txt_filter( get_the_content() ) );
    And that:
    $field = eae_encode_emails( txt_filter( get_field( 'field_name' ) ) );

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic