Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. //gettext files
  2. // languages/my-plugin-fr_FR.pot
  3. // languages/my-plugin-fr_FR.mo
  4.  
  5. //Javascript files
  6. // js/main.js
  7. (function($){
  8.  
  9. function getResources() {
  10. $.ajax({
  11. type: "POST",
  12. url: ajax_object.ajaxurl,
  13. dataType: "json",
  14. data: {
  15. action: 'get_resources'
  16. }
  17. })
  18. .done(function(data) {
  19. if (typeof data == "object" && data.hasOwnProperty("html")) {
  20. $(".my-selector").empty().html(data.html);
  21. } else {
  22. alert("error on server");
  23. }
  24. })
  25. .fail(function() {
  26. alert("error on server");
  27. });
  28. }
  29.  
  30. $(document).ready(function() {
  31. getResources();
  32. });
  33.  
  34. })(jQuery);
  35. // end js/main.js
  36.  
  37.  
  38. <?php
  39. //MyPlugin class
  40.  
  41. class MyPlugin {
  42. /// This value will be used as a unique identifier for translations
  43. public static $theme_domain_name = 'my-plugin';
  44.  
  45. public function init() {
  46. if (!is_admin()) {
  47. //any scripts and styles needed for the plugin to work in the front end
  48. add_action( 'wp_enqueue_scripts', array($this,'add_scripts_styles') );
  49. }
  50.  
  51. add_action('wp_ajax_get_resources', array($this,'ajax_get_resources'));
  52. add_action('wp_ajax_nopriv_get_resources', array($this,'ajax_get_resources'));
  53. }
  54.  
  55. public function ajax_get_resources() {
  56. $html = "";
  57.  
  58. //just an example of returning post objects
  59. $posts = get_posts();
  60.  
  61. $html .= $this->get_resources_html($posts);
  62.  
  63. echo json_encode(array('html'=>$html));
  64.  
  65. die();
  66. }
  67.  
  68. public function add_scripts_styles() {
  69. wp_register_script('main-js', plugin_dir_url(__FILE__) . 'js/main.js', array('jquery'), '20131023' );
  70. wp_enqueue_script('main-js' );
  71. wp_localize_script('main-js', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
  72. }
  73.  
  74. public function get_resources_html($resources) {
  75. $load_more_text = __('Load more',MyPlugin::$theme_domain_name);
  76.  
  77. //$html .= < do some other work on the resource posts provided >
  78.  
  79. $html .= <<<LOAD
  80. <a href="#">
  81. <span class="text-wrapper"><span class="text">
  82. {$load_more_text}
  83. </span></span>
  84. </a>
  85. LOAD;
  86.  
  87. return $html;
  88. }
  89. }
  90. ?>
  91.  
  92. <?php
  93. //root plugin file my-plugin.php
  94.  
  95. require_once dirname( __FILE__ ) .'/MyPlugin.php';
  96.  
  97. $MyPlugin = new MyPlugin();
  98.  
  99. add_action("plugins_loaded",function() {
  100. load_plugin_textdomain(MyPlugin::$theme_domain_name , false, dirname( plugin_basename(__FILE__) ) . '/languages/');
  101. });
  102.  
  103. $MyPlugin->init();
  104. ?>
  105.  
  106. /* if qTranslate is installed */
  107. /* set front locale for ajax calls requested from front-end */
  108.  
  109. function set_locale_for_frontend_ajax_calls() {
  110.  
  111. if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX
  112. && substr( $_SERVER['HTTP_REFERER'], 0, strlen( admin_url() ) ) != admin_url() ) {
  113.  
  114. load_theme_textdomain( 'your-theme-domain-name', get_template_directory() . '/languages’ );
  115. }
  116. }
  117.  
  118. add_action( 'admin_init', 'set_locale_for_frontend_ajax_calls' );
  119.  
  120. add_action('wp_head','jsURLs');
  121.  
  122. function jsURLs(){
  123.  
  124. global $q_config;
  125.  
  126. ?><script type="text/javascript">
  127. /* <![CDATA[ */
  128. var ajaxurl = "<?php echo admin_url('admin-ajax.php?lang='.$q_config['language']); ?>";
  129. /* ]]> */
  130. </script><?php
  131.  
  132. }
  133.  
  134. /* if qTranslate is not installed */
  135. /* set front locale for ajax calls requested from front-end */
  136.  
  137. function set_locale_for_frontend_ajax_calls() {
  138.  
  139. if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX
  140. && substr( $_SERVER['HTTP_REFERER'], 0, strlen( admin_url() ) ) != admin_url() ) {
  141. setlocale(LC_ALL, $_GET['lang']);
  142. load_theme_textdomain( 'your-theme-domain-name', get_template_directory() . '/languages’ );
  143. }
  144. }
  145.  
  146. add_action( 'admin_init', 'set_locale_for_frontend_ajax_calls' );
  147.  
  148. add_action('wp_head','jsURLs');
  149.  
  150. function jsURLs(){
  151.  
  152. global $q_config;
  153.  
  154. ?><script type="text/javascript">
  155. /* <![CDATA[ */
  156. var ajaxurl = "<?php echo admin_url('admin-ajax.php?lang='.get_locale()); ?>";
  157. /* ]]> */
  158. </script><?php
  159.  
  160. }
  161.  
  162. <?php
  163. global $sitepress;
  164. $current_language = $sitepress->get_current_language(); // get current language
  165. ?>
  166.  
  167.  
  168.  
  169. (function($){
  170.  
  171. function getResources() {
  172. $.ajax({
  173. type: "POST",
  174. url: ajax_object.ajaxurl,
  175. dataType: "json",
  176. data: {
  177. action: 'get_resources',
  178. lang: <?php echo $current_language; ?>
  179. }
  180. })
  181.  
  182.  
  183. [...]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement