最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

onClick inside post_content does not appear in post tinymce editor

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Mar 30, 2018 at 7:26 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Mar 29, 2018 at 16:00 J.BizMaiJ.BizMai 9002 gold badges10 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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;
}
发布评论

评论列表(0)

  1. 暂无评论