[wp-trac] [WordPress Trac] #26681: Allow HTML in selection captured with Press This bookmarklet
WordPress Trac
noreply at wordpress.org
Tue May 26 19:35:08 UTC 2015
#26681: Allow HTML in selection captured with Press This bookmarklet
-------------------------+------------------------------
Reporter: grosbouff | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Press This | Version: 3.8
Severity: normal | Resolution:
Keywords: close | Focuses:
-------------------------+------------------------------
Comment (by Stagger Lee):
Just tested last Press This (core). This is not usable at all.
100 times faster to just copy/paste text via TinyMce "Paste as text"
button and format it quickly.
All is one looooong line with all empty spaces removed. (with blockquote
or without)
How to format this text after ? No way. Exactly as you see it here looks
in Press This window, and after in Post.
-------------------------------------------------------------------------------
Adding multiple actions and filters using anonymous functions in
WordPressLeave a reply Using closures when adding hooks and filters in
WordPress is very convenient, instead of defining a global function to run
your hook, like this:123456 add_filter('the_content',
'filter_the_content');function filter_the_content($content){ //Do
something with the content return $content;}You can instead define the
function in-place, which is referred to as a closure, or anonymous
function:12345 add_filter('the_content', function($content){ //Do
something with the content return $content;});This is nice because it’s
slightly shorter and we avoid polluting the global namespace with
functions.But, you run into a problem if you want to attach the same
anonymous function to multiple filters. While you can do it very easily
with a global function like this:123456789 //Both the_content and
widget_text will be filtered through
filter_the_content()add_filter('the_content',
'filter_the_content');add_filter('widget_text', 'filter_the_content');
function filter_the_content($content){ //Do something with the content
return $content;}…there’s really no way to do this with anonymous
function. You’d have to create the same function twice, resulting in a DRY
violation:01020304050607080910111213 //Filter
the_contentadd_filter('the_content', function($content){ //Do something
with the content return $content;}); //Filter
widget_textadd_filter('widget_text', function($content){ //Do something
with the content return $content;});That’s not very pretty, so I thought
for a while about a good way to attach multiple filters and actions to the
same anonymous function.The solutionBy implementing a wrapper function for
add_filter(), which we will call add_filters(), we can let developers pass
an array as the filter name and then apply the filter function multiple
times, each time attaching our anonymous function. If we take the previous
example which forced us to repeat ourselves, we can now do this:12345
add_filters(array( 'the_content', 'widget_text' ), function($content){
//Do something with both the_content and widget_text filters return
$content;});Cool, but what about different priorities?Let’s fix that too!
If you have to apply different priorities, we’ll just specify them as an
array in the same order as the array of filter names, like this:123456
add_filters(array( 'the_content', 'widget_text' ), function($content){
//Do something with both the_content and widget_text filters return
$content;},array(15,20)); //the_content filter will have priority 15 and
widget_text filter will have priority 20How would we do this for
actions?Filters and actions are actually the same thing, the only
difference is that an action does not (generally) return anything, it just
lets you hook in and run your code at a certain event or timing. (Like
when a user registers, when a comment is posted, and so on.) In the
WordPress core source code, add_action() is just a wrapper for
add_filter()!For example, this code will print a comment in both the
header and footer.1234 add_actions(array('wp_head', 'wp_footer'),
function() { echo ""; }); So, how do you get these functions? You can
either download the helper plugin on GitHub, or copy and paste the below
code into a WordPress theme or
plugin!0102030405060708091011121314151617181920212223242526272829303132333435363738
/** * Add multiple filters to a closure * * @param $tags * @param
$function_to_add * @param int $priority * @param int $accepted_args * *
@return bool true */function add_filters($tags, $function_to_add,
$priority = 10, $accepted_args = 1){ //If the filter names are not an
array, create an array containing one item if(!is_array($tags)) $tags =
array($tags); //For each filter name foreach($tags as $index => $tag)
add_filter($tag, $function_to_add, (int)(is_array($priority) ?
$priority[$index] : $priority), (int)(is_array($accepted_args) ?
$accepted_args[$index] : $accepted_args)); return true;} /** * Add
multiple actions to a closure * * @param $tags * @param $function_to_add *
@param int $priority * @param int $accepted_args * * @return bool true
*/function add_actions($tags, $function_to_add, $priority = 10,
$accepted_args = 1){ //add_action() is just a wrapper around add_filter(),
so we do the same return add_filters($tags, $function_to_add, $priority,
$accepted_args
--
Ticket URL: <https://core.trac.wordpress.org/ticket/26681#comment:15>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list