Drilling a Hole in the Universe with WP_Query in a Shortcode

To condense a long prologue into a thesis statement, it is quite possible to output the results of a WordPress “query loop” via Shortcode, but doing so risks drilling a hole in spacetime: Placing a post whose content would include a version of the post itself (containing a version of the post itself, and so on) produces an overload and site crash.1

I first encountered this danger by accident, and initially wondered if the problem had something to do with specific query “arguments” or with shortcodes or the WordPress main “Loop” as such. In short, it’s just another example of the usual “infinite” regress error. I’ve also realized that under prior versions of the plug-in I’ve been working on, an inadvertent site-crash via infinite-looping was always a danger, presuming the right wrong move by some user.

To solve this problem or avoid this possible danger in the future, I’ve added a short sub-routine that excludes any offending post, or any offending post content, from whatever loop-within-a-loop the plug-in happens to output.

SIMPLE SOLUTION

The easiest and simplest solution seems to just to exclude all posts containing the shortcode from the loop-within-the-loop. Inserting the following within the new query loop or loop-within-the-loop (after $my_query->the_post() ; ), assuming a shortcode “tag” of “add_loopy_table,” suffices:

The above – or a slightly augmented version of it including “Excerpts” – for sites in which Shortcodes in Excerpts are allowed – is the solution I’m at this point intending to apply, for reasons I’ll get to, despite some limitations.

ALTERNATIVES

Another alternative is to allow inclusion of potentially infinitely offensive posts in whatever loop-within-a-loop, but to modify their content prior to display.

WordPress does include a strip_shortcodes() function that allows us to write a function like the following one, also employing has_shortcode(), but called when the loopy table actually gets to post content within its query:

The reason that in this approach we don’t remove the known-dangerous shortcode and leave any others alone is that strip_shortcodes() doesn’t accept parameters narrowing its strip. It’s full shortcode-Monty or nothing as far as strip_shortcodes() is concerned. That also seems to mean that oEmbedded tweets and videos will be ruined, apparently because they rely on an internalized shortcode functionality. 

To target the specific shortcode “Tag” (and variations) precisely, we’d need to parse the content, perhaps using some variation on WordPress’s own Regex, and a probably very reliable version could be written for the specific Tag (with or without arguments), with massaged and messaged content returned. (The function would look like the stripper above, but with a preg_replace() on $content instead of strip_shortcodes().) We could also try supplying and forcing some other alternatives or chain of alternatives whenever the dangerous code appears: Display of excerpt unless also dangerous, whole or partial replacement with animated infinity sign while a “Nearer My God to Thee” audio plays… That kind of thing.

The potential drawback to such solutions, special effects notwithstanding, would involve possible concern over even edgier cases in which a user wanted to include posts containing not-actually-infinitely-loopy tables. To enable these users, I think I’d either have to be able to test whether the potentially infinitely-loopy table was really a bad one, or introduce a settings option, but no practical as well as user-proof way of performing the test occurs to me, and anything requiring the user to follow instructions, or check or uncheck a box, etc., and possibly relying on a new taxonomy or post meta or more, still probably risks accidental inclusions of an actually infinitely-loopy table along with other loose ends, and altogether represents an investment of time and effort on something that may not ever make a difference to anyone.

KEEPING IT USER-PROOF

Maybe someone who has for some reason read this far has a bright and simple additional idea to share. For now, I’m leaning to the first, “has_shortcode/continue” version, because it’s simple and seems most user-proof: Posts that display potentially infinitely loopy tables won’t be displayed, period.2  As far as my current work goes, I think that’ll be the rule. Users will simply be informed that posts using the shortcode will be excluded from the output, and anyone who contrives some more exotic way of achieving the same effect can be directed to this post.

Until and unless someone (including possibly me) speaks up in favor of special exceptions, I’ll probably leave things that way.

Notes:

  1. ERR_CONNECTION_RESET []
  2. There is an additional complication, a very minor one in my view, having to do with the way that WP_Query counts iterations: In the presumably very rare cases where the Loop has to be “continued,” the quashed post will still “count” as a post, meaning that a display meant to display 10 posts will in fact display 9, but count 10. The logic required to overcome this rarity, and to explain to users how to avoid it, seems even further beyond the borders of the remotely likely significant. []

WordPresser
Home Page  Public Email  Twitter  Facebook  YouTube  Github   

Writing since ancient times, blogging, e-commercing, and site installing-designing-maintaining since 2001; WordPress theme and plugin configuring and developing since 2004 or so; a lifelong freelancer, not associated nor to be associated with any company, publication, party, university, church, or other institution. 

Posts in this series

Commenter Ignore Button by CK's Plug-Ins

Leave a Reply

Your email address will not be published. Required fields are marked *

*

WordPress Plug-In Notes

  1. Realizing the Commentariat (May 8, 2015)
  2. Child of Mog; Extraordinary Comments (May 25, 2015)
  3. Patronize 'Em: WordPress Draft Post Docket with Subscription and Donation Options (June 9, 2015)
  4. Realizing The Commentariat: Phase 2 (June 22, 2015)
  5. Pseudo-Redacting Spoilerer (July 25, 2015)
  6. Spoiling you some more (August 5, 2015)
  7. Testing Ajaxified Comments - Experiment Halted (August 11, 2015)
  8. New New Since Last Visit Comments Comments (August 16, 2015)
  9. WordPress Comment Nesting Unbound (August 22, 2015)
  10. The Snake Is Implemented (August 25, 2015)
  11. Comments Since Last Visit Reloaded, Reloaded, Testing Post (August 31, 2015)
  12. Comments Since Last Visit, Reloaded, Augmented, Installed, In Two Steps (September 13, 2015)
  13. Coming Soon (I Think!): Author Bios (September 25, 2015)
  14. How to Do Backlinking Footnotes (November 30, 2015)
  15. Who or What Is Using "Commenter Archive" and "commenter-thread"? (February 16, 2016)
  16. Enabling WordPress Press This for HostGator Sites (March 9, 2016)
  17. Linkback Your Xpost: A Simple WordPress Filter Function (March 14, 2016)
  18. Add Amazon Affiliate Tags to WordPress Posts <i>and</i> Comments Automatically (March 19, 2016)
  19. Finding Lost WordPress Widgets after Core Upgrade (March 21, 2016)
  20. Plug-In Away... and the Iron Law of Irony (April 16, 2016)
  21. To o-b or not to o-b (output-buffering in WordPress) - UPDATED (April 24, 2016)
  22. Output-Buffering and Extensible WordPress Plug-Ins (May 21, 2016)
  23. Getting Right with Image Rights: WP Replace Unlicensed and Broken Images Plug-In (June 17, 2016)
  24. Getting Right with Image Rights: Workflow and Major Minor Upgrade (June 27, 2016)
  25. Getting to Better WordPress Twitter oEmbed (June 28, 2016)
  26. An Alliance of Digital Artists (Art and Work in the Age of Instant Reproducibility) (July 8, 2016)
  27. Comparative Page Loads with and without Image Errors (July 14, 2016)
  28. jQuery-Filling an Input Box in WordPress Admin (July 15, 2016)
  29. Drilling a Hole in the Universe with WP_Query in a Shortcode (September 1, 2016)
  30. Troll-Stomping and Other Sensible Things: #WordPress Plug-In Beta Test/Preview (November 12, 2016)
  31. Commenter Ignore Button Preview Video (November 30, 2016)
  32. Is This Solution for Caches vs Cookies Going to Get Me in Trouble? (November 30, 2016)
  33. Commenter Ignore Button 0.99 (December 21, 2016)
  34. Adding wp.media Multiple Image Selection to WordPress Plug-Ins (January 5, 2017)
  35. Working around an Unexplained Failure of WordPress {$taxonomy} Hooks (January 10, 2017)In Progress: Subscribe!
  36. Better Twitter Embeds 2: Stripping the Convo for the Sake of the Convo (February 19, 2017)

Noted & Quoted

This is a Waterloo moment for Trump, the tea party and their alliance.  They have been stopped in their tracks not only by Democratic opposition but because of a mutiny within their own ranks. Although never particularly liked or respected, it is now clear that they are no longer feared. The bankruptcy of their ideas and their incompetence have been exposed. Their momentum has been dissipated. Their rejection of political norms has itself been scorned. Our long national nightmare may finally be coming to an end.

Comment →

One seasoned Democrat told me that among the reasons Trump won in 2016 was that a long year of Crooked Hillary talk, about emails and Goldman Sachs and the like, had steadily demoralised and demobilised the liberal base. If sustaining fury at Trump helps keep those same voters energised, so they eventually turn out to defeat him, it’ll be worth it, he says.

But it can’t just be in the form of world-weary, if witty, tweets. What’s needed is a coherent argument, one that explains why Trump’s repulsive behaviour matters. For Americans, that will surely centre on the state of their society. The civic realm is being degraded by Trump’s lies, vanities and insults. The national conversation is being coarsened. The basic democratic assumption, that disagreements can be resolved through discussion rather than coercion and violence, is being eroded from the very top. Note the language of Scaramucci’s outburst: “I want to fucking kill all the leakers.”

Comment →

[C]limate scientists have a strange kind of faith: We will find a way to forestall radical warming, they say, because we must.

It is not easy to know how much to be reassured by that bleak certainty, and how much to wonder whether it is another form of delusion; for global warming to work as parable, of course, someone needs to survive to tell the story. The scientists know that to even meet the Paris goals, by 2050, carbon emissions from energy and industry, which are still rising, will have to fall by half each decade; emissions from land use (deforestation, cow farts, etc.) will have to zero out; and we will need to have invented technologies to extract, annually, twice as much carbon from the atmosphere as the entire planet’s plants now do. Nevertheless, by and large, the scientists have an enormous confidence in the ingenuity of humans — a confidence perhaps bolstered by their appreciation for climate change, which is, after all, a human invention, too. They point to the Apollo project, the hole in the ozone we patched in the 1980s, the passing of the fear of mutually assured destruction. Now we’ve found a way to engineer our own doomsday, and surely we will find a way to engineer our way out of it, one way or another. The planet is not used to being provoked like this, and climate systems designed to give feedback over centuries or millennia prevent us — even those who may be watching closely — from fully imagining the damage done already to the planet. But when we do truly see the world we’ve made, they say, we will also find a way to make it livable. For them, the alternative is simply unimaginable.

Comment →
CK's WP Plugins

State of the Discussion

+ BTW, I recently upgraded some this and that on the back end of the blog, and it does seem to make comments post much faster [. . .]
Gutenberg: The Invention of the Printing Press, the Destruction of WordPress

For WordPress self-hosted people, there is already a "restore legacy editor" plugin, even though Gutenberg hasn't been installed yet as the default.

Gutenberg: The Invention of the Printing Press, the Destruction of WordPress
+ I thought you were on WordPress.com, not self-hosted WordPress. I can't find any info on WordPress.com and Gutenberg or Gutenbergerish editing, so I don't know [. . .]
Gutenberg: The Invention of the Printing Press, the Destruction of WordPress

Extraordinary Comments

CK's WP Plugins

Categories

Related