├── .gitignore ├── screenshot-1.png ├── screenshot-2.png ├── templates ├── psp-page-meta.php └── psp-page-list.php ├── page-shortcodes.php ├── readme.txt └── lib └── PageShortcodesPlugin.php /.gitignore: -------------------------------------------------------------------------------- 1 | .svn 2 | .settings 3 | .buildpath 4 | .project 5 | -------------------------------------------------------------------------------- /screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dflydev/page-shortcodes/master/screenshot-1.png -------------------------------------------------------------------------------- /screenshot-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dflydev/page-shortcodes/master/screenshot-2.png -------------------------------------------------------------------------------- /templates/psp-page-meta.php: -------------------------------------------------------------------------------- 1 |
-------------------------------------------------------------------------------- /templates/psp-page-list.php: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /page-shortcodes.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Page Shortcodes Plugin === 2 | Contributors: dflydev 3 | Donate link: http://dflydev.com/d2code/wordpress/page-shortcodes-plugin/ 4 | Tags: page, shortcode, shortcodes 5 | Requires at least: 2.8.6 6 | Tested up to: 3.0.1 7 | Stable tag: 0.1 8 | 9 | Embed page lists and other page information in pages and posts. 10 | 11 | == Description == 12 | 13 | Follow here: [Page Shortcodes](http://dflydev.com/d2code/wordpress/page-shortcodes-plugin/) 14 | 15 | Embed page lists and other page information in pages and posts by way of 16 | several shortcodes. 17 | 18 | `

[page_title name=my-page-slug]

` 19 | `[page_list name=my-page-slug]` 20 | 21 | = Shortcodes = 22 | 23 | The available shortcodes are: 24 | 25 | 1. [page_list] 26 | 1. [page_title] 27 | 1. [page_content] 28 | 1. [page_meta] 29 | 1. [page_permalink] 30 | 31 | = Common attributes = 32 | 33 | For all shortcodes, the "name" or "id" attributes must be specified. 34 | 35 | * "name" is the slug name for the pathed page. If your page is /foo/bar/baz, 36 | then specify name as 'foo/bar/baz'. 37 | * "id" is the ID for the page. 38 | 39 | = [page_list] attributes = 40 | 1. "template" attribute 41 | * The name of the template to use for this page list. 42 | 43 | = [page_meta] attributes = 44 | 1. "template" attribute 45 | * The name of the template to use for this page meta. 46 | 1. "meta" attribute 47 | * The name of the meta field to include. 48 | 49 | == Installation == 50 | 51 | 1. Download the plugin zip file 52 | 1. Unzip contents of plugin zip file 53 | 1. Upload the page-shortcodes directory to the `/wp-content/plugins/` directory 54 | 1. Activate the plugin through the 'Plugins' menu in WordPress 55 | 56 | == Frequently Asked Questions == 57 | 58 | = Can I customize how the page list looks? = 59 | 60 | Yes. Create a `ps-page-list.php` template in your theme. 61 | 62 | = Can I create a custom template for page lists? = 63 | 64 | Yes. Give a `template` attribute to your page list shortcode to specify which 65 | page list template to use. 66 | 67 | `[page_list name=my-page-slug template=my-custom-page-list]` 68 | 69 | = Can I customize how the page meta is displayed? = 70 | 71 | Yes. Create a `psp-page-meta.php` template in your theme. 72 | 73 | = Can I create a custom template for page metas? = 74 | 75 | Yes. Give a `template` attribute to your page meta shortcode to specify 76 | which page meta template to use. 77 | 78 | `[page_meta name=my-page-slug meta=some.meta.key template=my-custom-page-meta]` 79 | 80 | 81 | == Screenshots == 82 | 83 | 1. Editing a page to include content by way of the page shortcodes plugin. 84 | 2. Rendering of the previously shown page snippet. 85 | 86 | == Changelog == 87 | 88 | = 0.1 = 89 | * First release. 90 | 91 | == Upgrade Notice == 92 | 93 | = 0.1 = 94 | First release. -------------------------------------------------------------------------------- /lib/PageShortcodesPlugin.php: -------------------------------------------------------------------------------- 1 | pluginDir = dirname($pluginFile); 40 | add_action('init', array($this, 'handleWpAction_init')); 41 | } 42 | 43 | /** 44 | * Cache a page 45 | * @param std_object $page 46 | */ 47 | protected function cache($page = null) { 48 | if ( $page ) { 49 | self::$CACHE_BY_ID[$page->ID] = $page; 50 | self::$CACHE_BY_NAME[$page->post_name] = $page; 51 | } 52 | } 53 | 54 | /** 55 | * Find a page 56 | * @param array $atts Shortcode attributes 57 | * @param mixed $content Fallback content 58 | */ 59 | protected function findPage($atts, $content = null) { 60 | extract(shortcode_atts(array( 61 | 'id' => null, 62 | 'name' => null, 63 | ), $atts)); 64 | $foundPage = null; 65 | if ( $id ) { 66 | if ( isset(self::$CACHE_BY_ID[$id]) ) return $CACHE_BY_ID[$id]; 67 | $theQuery = new WP_Query(array( 68 | 'post_type' => 'page', 69 | 'page_id' => $id, 70 | )); 71 | if ( count($theQuery->posts) ) { 72 | $foundPage = $theQuery->posts[0]; 73 | } 74 | } 75 | elseif ( $name ) { 76 | if ( isset(self::$CACHE_BY_NAME[$name]) ) return self::$CACHE_BY_NAME[$name]; 77 | $foundPage = get_page_by_path($name); 78 | } 79 | if ( $foundPage ) $this->cache($foundPage); 80 | return $foundPage; 81 | } 82 | 83 | /** 84 | * Handle the WordPress 'page_title' shortcode. 85 | */ 86 | function handleWpShortcode_page_title($atts, $content = null) { 87 | $page = $this->findPage($atts, $content); 88 | if ( $page ) return $page->post_title; 89 | return $content; 90 | } 91 | 92 | /** 93 | * Handle the WordPress 'page_content' shortcode. 94 | */ 95 | function handleWpShortcode_page_content($atts, $content = null) { 96 | $page = $this->findPage($atts, $content); 97 | if ( $page ) return $page->post_content; 98 | return $content; 99 | } 100 | 101 | /** 102 | * Handle the WordPress 'page_permalink' shortcode. 103 | */ 104 | function handleWpShortcode_page_permalink($atts, $content = null) { 105 | $page = $this->findPage($atts, $content); 106 | if ( $page ) return get_permalink($page->ID); 107 | return $content; 108 | } 109 | 110 | /** 111 | * Handle the WordPress 'page_meta' shortcode. 112 | */ 113 | function handleWpShortcode_page_meta($atts, $content = null) { 114 | extract(shortcode_atts(array( 115 | 'id' => null, 116 | 'name' => null, 117 | 'meta' => null, 118 | 'template' => null, 119 | ), $atts)); 120 | $page = $this->findPage(array('id' => $id, 'name' => $name), $content); 121 | if ( $page ) { 122 | $data = get_post_meta($page->ID,$meta); 123 | $templates = array(); 124 | if ( $template ) { 125 | $templates = $this->createTemplateSearchPath($page, $template, $templates); 126 | } 127 | $templates = $this->createTemplateSearchPath($page, 'psp-page-meta', $templates); 128 | $foundTemplate = locate_template($templates); 129 | if ( ! $foundTemplate ) { 130 | $foundTemplate = $this->localTemplate('psp-page-meta.php'); 131 | } 132 | return $this->includeTemplate($foundTemplate, array( 133 | 'pspPageMeta' => isset($data[0]) ? $data[0] : null, 134 | )); 135 | } 136 | return $content; 137 | } 138 | 139 | /** 140 | * Handle the WordPress 'page_list' shortcode. 141 | */ 142 | function handleWpShortcode_page_list($atts, $content = null) { 143 | extract(shortcode_atts(array( 144 | 'id' => null, 145 | 'name' => null, 146 | 'template' => null, 147 | ), $atts)); 148 | if ( $name ) { 149 | $page = $this->findPage(array('name' => $name)); 150 | if ( $page ) $id = $page->ID; 151 | } 152 | if ( ! $id ) return $content; 153 | $queryArgs = array('post_type' => 'page', 'post_parent' => $id, 'orderby' => 'title', 'order' => 'ASC', ); 154 | $theQuery = new WP_Query($queryArgs); 155 | $posts = $theQuery->posts; 156 | if ( count($posts) ) { 157 | foreach ( $posts as $foundPage ) $this->cache($foundPage); 158 | } 159 | $output = ''; 160 | 161 | $templates = array(); 162 | 163 | if ( $template ) { 164 | $templates = $this->createTemplateSearchPath($page, $template, $templates); 165 | } 166 | $templates = $this->createTemplateSearchPath($page, 'psp-page-list', $templates); 167 | $foundTemplate = locate_template($templates); 168 | if ( ! $foundTemplate ) { 169 | $foundTemplate = $this->localTemplate('psp-page-list.php'); 170 | } 171 | 172 | return $this->includeTemplate($foundTemplate, array( 173 | 'pspPageList' => $posts, 174 | )); 175 | 176 | } 177 | 178 | protected function createTemplateSearchPath($page, $templateName, $templates = null) { 179 | if ( $templates === null ) $templates = array(); 180 | $templates[] = implode('-', array('page', $page->post_name, $templateName . '.php')); 181 | $templates[] = implode('-', array('page', $page->ID, $templateName . '.php')); 182 | $templates[] = implode('-', array('page', $templateName . '.php')); 183 | $templates[] = $templateName . '.php'; 184 | return $templates; 185 | } 186 | 187 | protected function localTemplate($template) { 188 | return implode('/', array($this->pluginDir, 'templates', $template)); 189 | } 190 | 191 | /** 192 | * Include a template. 193 | * @param string $template 194 | * @param array $data 195 | */ 196 | protected function includeTemplate($template, $data = null){ 197 | if ( $data ) extract( $data, EXTR_SKIP ); 198 | ob_start(); 199 | include $template; 200 | $output = ob_get_contents(); 201 | ob_end_clean(); 202 | return $output; 203 | } 204 | 205 | /** 206 | * Handle the WordPress 'init' action. 207 | */ 208 | public function handleWpAction_init() { 209 | add_shortcode('page_permalink', array($this, 'handleWpShortcode_page_permalink')); 210 | add_shortcode('page_title', array($this, 'handleWpShortcode_page_title')); 211 | add_shortcode('page_content', array($this, 'handleWpShortcode_page_content')); 212 | add_shortcode('page_meta', array($this, 'handleWpShortcode_page_meta')); 213 | add_shortcode('page_list', array($this, 'handleWpShortcode_page_list')); 214 | } 215 | 216 | /** 217 | * We only ever want one instance of our plugin loaded. 218 | */ 219 | static public function SINGLETON($pluginFile) { 220 | if ( self::$INSTANCE === null ) { 221 | self::$INSTANCE = new PageShortcodesPlugin($pluginFile); 222 | } 223 | return self::$INSTANCE; 224 | } 225 | 226 | } 227 | 228 | ?> --------------------------------------------------------------------------------