bpiwowar / papercite

Bibtex plugin for wordpress
http://www.bpiwowar.net/software/papercite/
GNU General Public License v2.0
42 stars 47 forks source link

Switch to the wordpress shortcode API #90

Open thebackfisch opened 10 years ago

thebackfisch commented 10 years ago

hey everyone, i would like to use the papercite plugin to display several bibtex-files in a people directory. unfortunatly, the shortcodes from papercite are not working within the connections-plugin that i'm using, but other shortcodes do work (e.g. from teachpress). i looked into the source-files but i cant figure out how pipercite handles shortcodes. it seems to me, papercite is not using the official wordpress-api (e.g. add_shortcodes) and therefor it's not working within the pugin.

maybe someone can fix this or explain me how papercite handles shortcodes.

bpiwowar commented 10 years ago

this is true - papercite is not using the wordpress API at the moment. Do you think that would solve your problem? What is the connections-plugin doing?

thebackfisch commented 10 years ago

Yes, I'm quite sure switching to the wordpress API would solve my problem, because other shortcodes are working well. The connections-plugin creates a people/staff directory. It would be nice if you could use shortcodes in the notes-field or the biographic info.

I've looked through the papercite code and read the wordpress doc and finally came up with a solution. I've changed the priority of the "the_content" hook from "-1" to "20", so the papercite parser is executed after the connections shortcodes were handled:

add_filter('the_content', 'papercite_cb', -1); to add_filter('the_content', 'papercite_cb', 20);

I hope this wont break anything else. I'm just curios why the priority was set to "-1"?

bpiwowar commented 10 years ago

Priority is a tricky thing, I remember I had to do this so that some other formatting modules would not break the shortcodes. Maybe switching to the shortcode API will avoid this trouble, so I will definitely explore this way for the next version.

matteoacrossi commented 9 years ago

I'd like the support of shortcode because I would like to use the [bibtex] code in a sidebar widget. In the meanwhile, do you know any workaround for my problem? For example, which PHP function may I call directly in place of the [bibtex ...] command?

Anyway, thank you for this great plugin!

bpiwowar commented 9 years ago

This will be done shortly - there are no other means to display citations for the moment.

saunus commented 9 years ago

I had the same problem running the siteorigin page-builder and the non-parsing of [bibtex ...] commands (and slimjetpack latex, too).

I am very thankful for thebackfisch's priority "hack". It works for the siteorigin page-builder, since its priority is set to default (10). I would suggest to use the Wordpress API for shortcodes to minimize compatibility problems.

Thank bpiwowar for the great plugin and thebackfisch for pasting his adjustment.

P.S: I hope I can persuade latex of working in this way, too

saunus commented 9 years ago

Although I think that this thread is un-google-able, I think I should share the outcome of last night and this day. I disassembled all the f*cking "the_content" and" text_widget" filters.

  1. It is a bad practice to play around with the priority of already applied filters. Although the hack above worked at first glance but if you add other shortcode plugins (and also some which filter the_content on their own way) the whole thing crashes and leaves you with invalid markup paragraphs. Furthermore: Don't freak out about wpautop. It does a really good job at its default priorityand with the right input.
  2. siteorigin uses widgets, which are filtered at run-time by "text_widget" filters (for debuggers: parsed before the_content even when not requested and they can not be echoed. I wrote them to a file for debugging. This filtering includes

    1 pll__
    2 [prefixed ]wpml_widget_text
    4 [prefixed]autoembed
    6 [prefixed]convert_smilies
    8 [prefixed]wpautop
    10 [prefixed]do_shortcode

    These prefixed filters seem to be applied by the "black-studio-tinymce-widget". I think they are there to prevent coders from removing them/changing priorities (prefixes are generated at run-time). Additionally it seems like "black-studio-tinymce-widget" can decide wether to execute the function. I actually had to apply an additional "add_filter('widget_text', 'wpautop',7);" to get the thing working.

  3. Site-origin assembles the widgets at priority 10 in "the_content" after wpauto and shortcode_unautop. This means that if you want to parse widget-text like you do it with normal content, you have to complement the prior filtering to "widget_text". In my case:

    add_filter('widget_text', 'wptexturize', 5);
    add_filter('widget_text', 'convert_chars', 7);
    add_filter('widget_text', 'wpautop',7);
    add_filter('widget_text', 'shortcode_unautop', 9);
  4. For additional plugins you have to search for the relative hook position:

    native: add_filter( 'the_content', 'papercite_cb',-1 );
    function.php: add_filter('widget_text', 'papercite_cb', 3);
    
    native: add_filter( 'the_content', 'papercite_cb',-1 );
    function.php: add_filter('widget_text', 'papercite_cb', 3);
    
    native: add_filter( 'the_content', 'latex_markup', 9 );
    function.php:add_filter('widget_text', 'latex_markup', 5); //!!!before wptexturize!!!!
  5. I also use the custom filtering "shortcode_empty_paragraph_fix" (google it)

    add_filter('the_content', 'shortcode_empty_paragraph_fix',10); //before do shortcode
    add_filter('widget_text', 'shortcode_empty_paragraph_fix',9); //before do shortcode
  6. Alternative Approaches: a. remove all filters from widget-text and give siteorigin assembly priority -100.

    remove_all_filters( 'widget_text');
    remove_filter( 'the_content', 'siteorigin_panels_filter_content', 10 );
    add_filter( 'the_content', 'siteorigin_panels_filter_content', -100 );

    Gives Bullshit for all the other Widgets (Sliders parsed through wpautop) and the text-widget can not be used as widget anymore b. automatically add all filtering from the_content to text_widget

     remove_all_filters( 'widget_text');
     function apply_the_content_to_widget_text( $text ) {
       return apply_filters( 'the_content', $text );
     }
     add_filter( 'widget_text', 'apply_the_content_to_widget_text' );

    This ended for me in a loop i didn't analyze, possibly something because of prefixed filters.

Final thougths: If latex and papercite (and other content-filtering plugins) would use the wordpress shortcode api, you wouldn't need to manually hook anything into the "text_widget". Even shortcode_unauto p could do its job since it respects only registered shortcodes.