├── README.md ├── byscripts_ajax_posts_loader.php ├── js └── byscripts_ajax_posts_loader.js ├── lang ├── byscripts_ajax_posts_loader-fr_FR.mo └── byscripts_ajax_posts_loader-fr_FR.po └── templates └── settings.php /README.md: -------------------------------------------------------------------------------- 1 | Wordpress Ajax Posts Loader 2 | =========================== 3 | 4 | Description 5 | ----------- 6 | 7 | This [Wordpress](http://wordpress.org) plugin allows you to load the next page of posts with AJAX. 8 | 9 | It's based on Wordpress "posts per page" settings. 10 | 11 | If your blog displays 5 posts per page, each click on "Load more posts" will load 5 more posts. 12 | 13 | Configuration 14 | ------------- 15 | 16 | You will need to configure the plugin to suit your website structure. 17 | 18 | * **Content CSS selector**: It's the CSS selector of the element at the bottom of which the "Load more posts" link will be added. (Default: `#content`) 19 | 20 | * **Pagination CSS selector**: It's the CSS selector of the pagination element which will be removed when the plugin is loaded. (Default: `.pagination`) 21 | 22 | * **Remove link after last result**: When checked, the "Load more posts" link will be removed after retrieving the last results. Else its label is replaced with "No more posts to load" (Default: `false`) 23 | 24 | Installation 25 | ------------ 26 | 27 | 1. Upload the plugin in `/wp-content/plugins directory`. 28 | 2. Activate it through the 'Plugin' menu in Wordpress admin. 29 | 3. Go to `Settings > Ajax Posts Loader` to configure the plugin. 30 | 4. Open you CSS file and add styles to `#byscripts_ajax_posts_loader` element. 31 | 32 | Changelog 33 | --------- 34 | 35 | #### v0.5 36 | 37 | - Fix the plugin to works correctly with qTranslate 38 | 39 | #### v0.4 40 | 41 | - Add some description in admin page 42 | - Complete French translation 43 | 44 | #### v0.3 45 | 46 | - Better page link management. Should be compatible with any URL format. 47 | 48 | #### v0.2 49 | 50 | - Add option to remove link after the last results 51 | 52 | #### v0.1 53 | 54 | - First version 55 | 56 | License 57 | ------- 58 | 59 | This plugin is released under the [MIT License](http://opensource.org/licenses/MIT). -------------------------------------------------------------------------------- /byscripts_ajax_posts_loader.php: -------------------------------------------------------------------------------- 1 | identifier, 59 | plugin_dir_url(__FILE__) . 'js/' . $this->identifier . '.js', 60 | array('jquery'), 61 | '1.0', 62 | true 63 | ); 64 | 65 | // Max number of pages 66 | $page_number_max = $wp_query->max_num_pages; 67 | 68 | // Next page to load 69 | $page_number_next = (get_query_var('paged') > 1) ? get_query_var('paged') + 1 : 2; 70 | 71 | // Add some parameters for the JS. 72 | wp_localize_script( 73 | $this->identifier, 74 | $this->identifier, 75 | array( 76 | 'page_number_next' => $page_number_next, 77 | 'page_number_max' => $page_number_max, 78 | 'page_link_model' => get_pagenum_link(9999999999), 79 | 'load_more_str' => __('Load more news', $this->identifier), 80 | 'loading_str' => __('Loading...', $this->identifier), 81 | 'error_str' => __('An error occured. Could not load more posts.', $this->identifier), 82 | 'no_more_str' => __('No more news to load', $this->identifier), 83 | 'content_css_selector' => get_option($this->prefix('content_css_selector'), '#content'), 84 | 'pagination_css_selector' => get_option($this->prefix('pagination_css_selector'), '.pagination'), 85 | 'remove_link_after_last_result' => get_option($this->prefix('remove_link_after_last_result'), false) 86 | ) 87 | ); 88 | } 89 | 90 | public function loadTextDomain() { 91 | load_plugin_textdomain($this->identifier, false, dirname(plugin_basename(__FILE__)) . '/lang/'); 92 | 93 | // Small hack to make the string visible in PoEdit (I18n) 94 | __('Load the next page of posts with AJAX.', $this->identifier); 95 | } 96 | 97 | /** 98 | * Prefixes a string with an unique identifier 99 | * 100 | * @var string $str 101 | * @return string 102 | */ 103 | private function prefix($string) { 104 | return $this->identifier . '_' . $string; 105 | } 106 | 107 | public function adminInit() { 108 | $this->registerSettings(); 109 | } 110 | 111 | public function registerSettings() { 112 | register_setting($this->prefix('settings'), $this->prefix('content_css_selector')); 113 | register_setting($this->prefix('settings'), $this->prefix('pagination_css_selector')); 114 | register_setting($this->prefix('settings'), $this->prefix('remove_link_after_last_result')); 115 | } 116 | 117 | public function adminMenu() { 118 | add_options_page('ByScripts Ajax Posts Loader', 'Ajax Posts Loader', 'manage_options', $this->identifier, array($this, 'settingsPage') ); 119 | } 120 | 121 | public function settingsPage() { 122 | if(!current_user_can('manage_options')) { 123 | wp_die(__('You do not have sufficient permissions to access this page.', $this->identifier)); 124 | } 125 | 126 | include dirname(__FILE__) . '/templates/settings.php'; 127 | } 128 | 129 | public function settingsLink($links) { 130 | $link = sprintf('%s', $this->identifier, __('Settings', $this->identifier)); 131 | array_unshift($links, $link); 132 | return $links; 133 | } 134 | } 135 | 136 | new ByScriptsAjaxPostsLoader(); 137 | -------------------------------------------------------------------------------- /js/byscripts_ajax_posts_loader.js: -------------------------------------------------------------------------------- 1 | jQuery(document).ready(function($) { 2 | 3 | var page_number_next = parseInt(byscripts_ajax_posts_loader.page_number_next), // The number of the next page to load (/page/x/). 4 | page_number_max = parseInt(byscripts_ajax_posts_loader.page_number_max), // The maximum number of pages the current query can return. 5 | page_link_model = byscripts_ajax_posts_loader.page_link_model, // The link of the next page of posts. 6 | $content = $(byscripts_ajax_posts_loader.content_css_selector), // The content Element posts are inserted to 7 | $pagination = $(byscripts_ajax_posts_loader.pagination_css_selector), // The wordpress default pagination 8 | $loaderTrigger; 9 | 10 | /** 11 | * Replace the traditional navigation with our own, 12 | * but only if there is at least one page of new posts to load. 13 | */ 14 | if(page_number_next <= page_number_max) { 15 | // Insert the "More Posts" link. 16 | $content.append('