這個外掛並未在最新的 3 個 WordPress 主要版本上進行測試。開發者可能不再對這個外掛進行維護或提供技術支援,並可能會與更新版本的 WordPress 產生使用上的相容性問題。

Priority Shortcodes

描述

Ever get annoyed or frustrated by the way TinyMCE+wpautop+do_shortcode wreak havoc on your shortcodes, wrapping <p>s around <div>s, leaving orphan </p>s, and all sorts of craziness? This plugin allows you to specify shortcodes to be processed before those other actions, generating the clean code you expected.

Priority Shortcodes works by adding an action to the_content and widget_text hooks with a higher priority than wpautop and do_shortcode. The result is that those other actions run your shortcode’s final output, rather than trying to guess if it should be wrapped in <p> tags, etc.

The Codex says:

wpautop recognizes shortcode syntax and will attempt not to wrap p or br tags around shortcodes that stand alone on a line by themselves. Shortcodes intended for use in this manner should ensure that the output is wrapped in an appropriate block tag such as <p> or <div>.

But sometimes shortcodes stand on their own on a line, wanting desperately to be wrapped in a paragraph tag, and sometimes they don’t. Like a shortcode that generates a <span> tag with some classes. (Yeah, you might want to use custom TinyMCE styles instead for a simple span.)

Usage

Where you want to process a shortcode with priority, use “[!” at the start. For example: [my-shortcode] becomes [!my-shortcode] and [my-shortcode]Some content.[/my-shortcode] becomes [!my-shortcode]Some content.[/my-shortcode].

Gutenberg

Users of the new WordPress editor, “Gutenberg”, will probably not use Priority Shortcodes within the new editor, but the plugin continues to work in the Classic Editor.

安裝

See the standard installation instructions at WordPress.org or WPBeginner.

I expect developers will copy the code directly into their functions.php files, possibly customizing the string to specify the priority shortcodes.

常見問題

Does this affect all shortcodes?

No.

Should I use this everywhere instead of the normal shortcodes?

No. Only use it on shortcodes that are being messed up.

How does it work?

The plugin adds an action hook to the_content with priority 9, before wpautop and do_shortcode, which run with normal priority 10. The function that runs in our action hook is basically a copy/mashup of do_shortcode() and do_shortcode_tag() with a custom regular expression. That regex pulled using get_shortcode_regex(), so any updates to that expression in core will be respected by this plugin. See shortcodes.php

Why not just change the order wpautop and do_shortcode run in?

That can mess up other shortcodes. Plus, I’ve tried it, and it doesn’t have the result I wanted at all.

評價

2022年12月13日
This plugin has a fatal flaw for me so I'm giving it a 1/5, from me. Karma sucks!
閱讀全部0個評價

貢獻者及開發者

“Priority Shortcodes” 是一個開源的軟體。以下的人對這個外掛作出了貢獻。

貢獻者

將 Priority Shortcodes 外掛本地化為台灣繁體中文版。

對開發相關資訊感興趣?

任何人均可瀏覽程式碼、查看 SVN 存放庫,或透過 RSS 訂閱開發記錄

修改日誌

2.0.2

  • Tested with WP 5.6
  • Updated README with note about Gutenberg

2.0.1

  • Bumped “Tested up to” version

2.0

  • Changed to more simple [! syntax
  • Added compatibility with text widgets

1.0

  • Initial release