Вопрос

I'm doing a migration. I made a PHP script to generate posts in the new WordPress database. The post content contains an HTML link containing href="javascript:" and onClick="trackOutboundLink('url')".

post_content in the new database

<div>
  <a class="exclusive button-exclusive" href="javascript:" onclick="trackOutboundLink('url'); return false;" rel="nofollow"><span>&#9655; Text link</span></a>
</div>

For some reason, the href and onclick attributes did not show in the admin post editor. Why? How can I fix this?

Это было полезно?

Решение

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top