onClick inside post_content does not appear in post tinymce editor

By default, TinyMCE sets allow_script_urls to false, which is what is causing the href attribute and value in the link to be removed.

Also in the interest of best practices, TinyMCE does not allow the onlcick attribute on links by default. This can be changed too.

The tiny_mce_before_init hook can be used to change TinyMCE’s options so that this content is not removed:

/**
 * Filters the TinyMCE config before init.
 *
 * @param array  $mceInit   An array with TinyMCE config.
 * @param string $editor_id Unique editor identifier, e.g. 'content'.
 */
add_filter( 'tiny_mce_before_init', 'wpse_tiny_mce_before_init', 10, 2 );
function wpse_tiny_mce_before_init( $mceInit, $editor_id ) {
    // Allow javascript: in href attributes.
    $mceInit['allow_script_urls'] = true;

    // Allow onclick attribute in anchor tags.
  if ( ! isset( $mceInit['extended_valid_elements'] ) ) {
        $mceInit['extended_valid_elements'] = '';
    } else {
        $mceInit['extended_valid_elements'] .= ',';
    }
    $mceInit['extended_valid_elements'] .= 'a[href|rel|class|id|style|onclick]';

    return $mceInit;
}

Leave a Comment