Title: Processing Shortcode
Last modified: August 22, 2016

---

# Processing Shortcode

 *  [Matt](https://wordpress.org/support/users/syntax53/)
 * (@syntax53)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/processing-shortcode/)
 * So home come your plugin doesn’t process shortcode? I did a bunch of searching
   around and was amazed that you don’t do it. I tried to build a class extension
   to do it but couldn’t figure out a way to hook into your plugin or extend the
   class in between the initialization and the processing of the content.
 * A simple modification to the “get_field” function of the blcContainer container
   class adds shortcode processing:
 * > $value = $w->$field;
   >  if (!empty($value) && $field == ‘post_content’) $value
   > = do_shortcode($value); return $value;
 *     ```
       function get_field($field = ''){
       		if ( empty($field) ){
       			$field = $this->default_field;
       		}
   
       		$w = $this->get_wrapped_object();
       		$value = $w->$field;
       		if (!empty($value) && $field == 'post_content') $value = do_shortcode($value);
       		return $value;
       	}
       ```
   
 * I found 3 broken links that the plugin didn’t pick up because they were generated
   by shortcode (something from a [global content block](https://wordpress.org/plugins/global-content-blocks/)
   in this case). If there is a good reason not to process shortcode, could you 
   make it an option to do so and/or provide a place to hook into or extend a class?
 * [https://wordpress.org/plugins/broken-link-checker/](https://wordpress.org/plugins/broken-link-checker/)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Janis Elsts](https://wordpress.org/support/users/whiteshadow/)
 * (@whiteshadow)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/processing-shortcode/#post-5918907)
 * > So home come your plugin doesn’t process shortcode? I did a bunch of searching
   > around and was amazed that you don’t do it.
 * It does support shortcodes, but only in link and image URLs. For example, this
   would work:
    `<a href="[myshortcode]">link text</a>`
 * It doesn’t support shortcodes in other places because it would break a bunch 
   of things: link editing, the ability to remove links without manually updating
   each post, the ability to add a strike-through effect to broken links, and so
   on.
 * To illustrate, lets say your suggested patch is implemented. How would you handle
   the following situations:
    - What happens when you try to edit one of those shortcode links via BLC? There
      is no actual link to edit, so simply replacing the URL won’t work. Should 
      the plugin somehow convert the new link back to a shortcode? (If so, how?)
    - How about removing links? The plugin can’t just remove the shortcode because
      it doesn’t _have_ the shortcode – it just has the shortcode output.
    - Shortcodes can output arbitrary content. How about shortcodes that display
      a dozen different links? What should the plugin do when the user tries to 
      edit/unlink _one_ of those links?
 *  Thread Starter [Matt](https://wordpress.org/support/users/syntax53/)
 * (@syntax53)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/processing-shortcode/#post-5918910)
 * I think you should detect when a link is found within shortcode and then display
   those links differently on the broken link lists. Alert the user they were found
   in shortcode and can’t be fixed automatically. Ideally, at least direct the user
   to the post/page they were found in (right-most column that you already have).
 * The name of the plugin is “Broken Link Checker” and I don’t think you should 
   sacrifice the ability to find broken links simply because you can’t do magic 
   on all of them.
 *  [Janis Elsts](https://wordpress.org/support/users/whiteshadow/)
 * (@whiteshadow)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/processing-shortcode/#post-5918932)
 * > I think you should detect when a link is found within shortcode and then display
   > those links differently on the broken link lists.
 * Sure, that would be nice to have. How would that actually work, though?
 *  Thread Starter [Matt](https://wordpress.org/support/users/syntax53/)
 * (@syntax53)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/processing-shortcode/#post-5919117)
 * How bout this…
 * *ignore my first post and undo that change.
 * *Add a new column to your database: tiny_int (1 or 0) titled ‘found_in_shortcode’
   or something.
 * **containers.php–**
 * synch function:
 * Line 274: `$all_instances_found = array();`
 * Line 282: `if (!empty($found_instances)) { $all_instances_found = array_merge(
   $all_instances_found, $found_instances); }`
 * Line 291:
 *     ```
       if ($field == 'post_content' || (empty($field) && $this->default_field == 'post_content')) {
       	$shortcode_expanded = do_shortcode($value);
       	//Parse the field with each parser
       	foreach($parsers as $parser){
       		$found_instances_with_shortcode = $parser->parse( $shortcode_expanded, $base_url, $default_link_text );
   
       		//note sure if array_unique or something better could be used here
       		foreach($found_instances_with_shortcode as $key => $shortcode_instance) {
       			foreach($all_instances_found as $instance){
       				if ($shortcode_instance->raw_url == $instance->raw_url && $shortcode_instance->link_text == $instance->link_text) {
       					unset($found_instances_with_shortcode[$key]); break;
       				}
       			}
       		}
   
       		foreach($found_instances_with_shortcode as $shortcode_instance){
       			$shortcode_instance->found_in_shortcode = 1;
       			$shortcode_instance->set_container($this, $name);
       			$shortcode_instance->save();
       		}
       	}
       }
       ```
   
 * **instances.php–**
 * Line 27: `var $found_in_shortcode = 0;`
    Lines 236 and 275: update queries to
   write ‘found_in_shortcode’ value
 * Then on the admin form, pull and check that value. If “found_in_shortcode” ==
   1 then display a text “Embdedded in shortcode” and disable the links that are
   used to fix it.
 *  [Janis Elsts](https://wordpress.org/support/users/whiteshadow/)
 * (@whiteshadow)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/processing-shortcode/#post-5919120)
 * If I understand correctly, the overall algorithm is this:
    1. Find all links in a post. Store them for later use.
    2. Run shortcodes.
    3. Find all links again.
    4. Throw away any links that were already found in step #1.
    5. The links that are left were generated by shortcodes.
 * That looks like it would work. It would more than double the time it takes to
   parse a post, but that’s acceptable if it’s implemented as an opt-in feature.
   There are also ways to optimize it, like only doing the extra steps for posts
   that actually contain shortcodes, and using a map/hashtable to speed up the “
   did we see this instance already?” checks.
 * Thank you for actually providing a workable solution. I’ll add this to my to-
   do list for a future release.
 *  Thread Starter [Matt](https://wordpress.org/support/users/syntax53/)
 * (@syntax53)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/processing-shortcode/#post-5919121)
 * great

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Processing Shortcode’ is closed to new replies.

 * ![](https://ps.w.org/broken-link-checker/assets/icon-256x256.png?rev=2900468)
 * [Broken Link Checker](https://wordpress.org/plugins/broken-link-checker/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/broken-link-checker/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/broken-link-checker/)
 * [Active Topics](https://wordpress.org/support/plugin/broken-link-checker/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/broken-link-checker/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/broken-link-checker/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [Matt](https://wordpress.org/support/users/syntax53/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/processing-shortcode/#post-5919121)
 * Status: not resolved