Forum Replies Created

Viewing 15 replies - 1 through 15 (of 18 total)
  • I understand single-post-[slug].php or single-[post-type].php can let you separately apply single post templates. The problem is that the client doesn’t want to enter a page slug or select a custom post type for each post.

    You need something specify that this post belongs to one type and other to other type. maybe we can use for it post meta data.
    Something like this create for each post metadata that will default set in ‘type one’ and when user wants create second type he set checkbox or radiobutton and will get second type.

    In general here many variants, it doesn’t matter what realization you’ll chose.

    • This reply was modified 6 years, 8 months ago by neo332.

    1. you must determine ajax action something like this

    
    add_action( 'wp_ajax_'.'my_action', 'my_action' );
    add_action( 'wp_ajax_nopriv_'.'my_action', 'my_action' );
    

    2 determine function my_action

    
    function my_action(){
    echo "this is result";
    die;
    }
    

    3. then make ajax request pass like parameter action = my_action

    
    $.ajax({
     url: my_object.ajax_url,
     type: 'POST',
     data: {
       action: 'my_action',
       data: 'something',
     },
     success: function(msg){
       console.log(msg);
     },
    });
    

    my_object.ajax_url this is you must pass variable to your javascript script or determine explicitly like https://yourSiteName/wp-admin/admin-ajax.php.

    instead this

    
    url: my_object.ajax_url,
    

    add this

    
    url: 'https://yourSiteName/wp-admin/admin-ajax.php',
    

    or add code that will add variable to you javascript file using this code

    
    add_action( 'wp_enqueue_scripts', 'includeJavaScriptPublic' );
    function includeJavaScriptPublic(){
     wp_enqueue_script( 'your-js-script', plugins_url('/path/to/your/script', dirname(__FILE__)), array('jquery'), '1.0.0', true);
     wp_localize_script( 
       'your-js-script', 
       'my_object', 
        array(
         'ajax_url'=>admin_url('admin-ajax.php'), 
        ) 
     );
    }
    

    read better this documentations link

    I think you need to spend a few days and then you will know how to do it. It’s not easy for newbie, I’d spent a few day to understand how it works, and not so far don’t know how it works. always forget something…

    • This reply was modified 6 years, 8 months ago by neo332.
    • This reply was modified 6 years, 8 months ago by neo332.
    • This reply was modified 6 years, 8 months ago by neo332.
    • This reply was modified 6 years, 8 months ago by neo332.

    it seem wordpress doesn’t support REST for status parameter.
    Might be you can add status something like this &status=pedium and then retrieve this posts by yourself manually.

    I sometimes use directory where plugin constants self. You can use too. In root folder create /data folder.

    Thread Starter neo332

    (@neo332)

    I do in class namespace nameOfNamespace and use the same classes for all next classes, but I want to build my own framework for build wp plugins. It’s not useful use wp codex and always repeat to build the same code.

    I’m going to create libs from my own class that I will recopy in each plugins, something like this there exists wp crone, we create our class called WpCrone and put there our code, make incapsulationf ro existing wp functions and then we won’t google for repeat new code, simply we can read in file description of each our function and call it. It seems good idea.

    I have a few classes that I build but it not what I want, they are all mixed between reap wp functionality and mine, for each new project I reedit each file. Now want extract in folder wp-core all classes that will have only wp functionality and other classes new will extended from them. I think better use inheritance in perant classes will core in child changeable functionality.

    Thread Starter neo332

    (@neo332)

    I did this code:

    add_action('save_post', 'save_posta', 2, 10);
    function save_posta($post_id, $post, $update){
      echo $post_id;
      if ( wp_is_post_revision( $post_id ) )
        return;
    
      die('here we are');
    }

    and when press ‘add new’ got this:
    489here we are
    and when I update page it increment number, Why?
    Why does it work not like describe in cods link

    Thread Starter neo332

    (@neo332)

    I did it

    
    <?php 
    /**
    Plugin Name: ks-highlight-js
    Description: Description
    Plugin URI: https://kselax.ru
    Author: Author
    Author URI: https://kselax.ru
    Version: 1.0.0
    License: GPL2
    Text Domain: ks-highlight-js
    */
    namespace Ks;
    require_once dirname(__FILE__).'/core/class/KsHighlightJs.php';
    
    ?>
    

    this is didn’t help,

    only help when I directly put namespace ks; to Includes.php file, but I don’t what always reedit this file.

    Thread Starter neo332

    (@neo332)

    I like to call the add_shortcode function in __construct function of the class.

    it’s all in one class, if you have big plugin it won’t useful.

    for shortcode really that I used before is not well, will rebuild all without intermediate class

    Thread Starter neo332

    (@neo332)

    I think when we create class shortcode we should do two initialization at first for main class and second in handle function?

    Thread Starter neo332

    (@neo332)

    Ok, Thanks, I haven’t read yet that code, but I’ll do…
    code in wp documentation I mean.

    • This reply was modified 6 years, 11 months ago by neo332.
    Thread Starter neo332

    (@neo332)

    I’ve already seen it, I want other structure where will folder with classes that I’ll use in each other plugins, For example I created class CSV and I can use it in any other plugins, or class form for create form and again I won’t recreat it.

    In some classes I will use path to this plugin and each plugin has other paths, path easy find dirname(__FILE__).

    I have problem with url, it’s I’ve little confused theme.
    I’ve created class Included_files, that is manage included files and I need to get url for my included files now there I include jQueryUI, that I placed in /libs, folder,
    This class Included_files placed in folder /core/class/Included_files.php,

    For get url I did this code:

    function ini(){
          $this->url=plugin_dir_url(__FILE__)."../../libs";
        }

    url for /lib where will all others libs.
    is it normally use ../../ ??? because plugin_dir_url(__FILE__) return dir to my current file.

    I think probably should think self and create ideal structure. That structure that I saw in boilerplate seems not good. That is suite for procetural programming, for classes should to do other structure where will loaders of classes.

    I don’t know, I’ve done it the second time and as usual problems with paths. Can’t reuse next time the same code. I always change structure but I won’t to do it again….

    Thread Starter neo332

    (@neo332)

    I’m going to do it OOP style, but there only one example and file structure not like there. I’ll use my own structure /core /libs folders, and others files won’t like there in guide.

    Thread Starter neo332

    (@neo332)

    differences between file that provide with plugin and that is in faq
    https://prntscr.com/hmglke

    Is it deliberately did or it’s the occasional error?

    • This reply was modified 6 years, 11 months ago by neo332.
    Thread Starter neo332

    (@neo332)

    I’ve found why it didn’t work
    Test code from plugin comes with reference &$cachedata

    function dynamic_cache_test_filter( &$cachedata) {
    		return str_replace( DYNAMIC_CACHE_TEST_TAG, "<!-- Hello world at " . date( 'H:i:s' ) . " -->", $cachedata );
    	}

    I changed it without $cachedata and start work. Is it normally?

    Thread Starter neo332

    (@neo332)

    this is file dynamic-test-cache.php

    <?php
    
    /*
     * On the Advanced Settings page enable "Enable dynamic caching" and clear
     * the cache.
     *
     * Plugin authors: NEVER define the template tag for your users. Make them
     * choose one so it will be unique to their site.
     *
     * There are two examples in this file. Both use template tags that must be
     * kept secret.
     *
     * GLOSSARY:
     *
     * Dynamic content: the text or widget you want to show visitors to your site
     * that changes every time it's viewed.
     * Placeholder/template tag: the string of random characters placed in your
     * theme file or printed in an action where the dynamic content will go.
     * Output buffer (ob): any text that is printed by PHP to be sent to the browser
     * but captured by PHP for further manipulation.
     * OB Callback function: A function that is called when the output buffer is
     * filled with a html page. The contents of the page are passed to the function
     * for processing.
     *
     * **** MAKE SURE YOU KEEP THE TEMPLATE TAG SECRET ****
     */
    
    /*
     * EXAMPLE 1
     * https://ocaoimh.ie/2013/10/21/shiny-new-dynamic-content-wp-super-cache/
     * Replace a string in your theme with the dynamic content.
     *
     * dynamic_cache_test_init()
     * This function is the first one to be called. This function hooks
     * dynamic_cache_test_template() to the WordPress action, wp_footer.
     * This script is loaded before WordPress is and the add_action()
     * function isn't defined at this time.
     * This init function hooks onto the cache action "add_cacheaction"
     * that fires after WordPress (and add_action) is loaded.
     *
     *
     * dynamic_cache_test_template_tag()
     * This function hooks on to wp_footer and displays the secret template
     * tag that will be replaced by our dynamic content on each page view.
     *
     *
     * dynamic_cache_test_filter()
     * This function hooks on to the filter through which all the cached data
     * sent to visitors is sent.
     * In this simple example the template tag is replaced by a html comment
     * containing the text "Hello world at " and the current server time.
     * If you want to use the output of a WordPress plugin or command you
     * must enable "late init" on the settings page. Each time you reload
     * the cached page this time will change. View the page source to examine
     * this text.
     *
     * Chronology of a request:
     * 1. dynamic_cache_test_init() hooks dynamic_cache_test_template_tag() on
     *    to the wp_footer action. dynamic_cache_test_filter() is hooked on to
     *    the wpsc_cachedata filter.
     * 2. An output buffer is created by WP Super Cache.
     * 3. Most of the page is generated by WordPress.
     * 4. The wp_footer action fires and the TAG is printed to the page.
     * 5. Processing continues and the page is created.
     * 6. The output buffer finishes. A WP Super Cache callback function runs
     *    and saves the output buffer to a cache file. The wpsc_cachedata 
     *    filter is called.
     * 7. The function dynamic_cache_test_filter() runs and replaces the TAG in
     *    the buffer with the "Hello world" string.
     * 8. The output buffer is pushed to the browser to be displayed.
     */
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    
    define( 'DYNAMIC_CACHE_TEST_TAG', 'kkk' ); // Change this to a secret placeholder tag
    if ( DYNAMIC_CACHE_TEST_TAG != '' ) {
    	error_reporting(E_ALL);
    	function dynamic_cache_test_safety( $safety ) {
    		return 1;
    	}
    	add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_cache_test_safety' );
    
    	function dynamic_cache_test_filter( &$cachedata) {
    		return 'hellow';
    		return str_replace( DYNAMIC_CACHE_TEST_TAG, "<!-- Hello world at " . date( 'H:i:s' ) . " -->", $cachedata );
    	}
    	add_cacheaction( 'wpsc_cachedata', 'dynamic_cache_test_filter' );
    
    	function dynamic_cache_test_template_tag() {
    		echo DYNAMIC_CACHE_TEST_TAG; // This is the template tag
    	}
    
    	function dynamic_cache_test_init() {
    		add_action( 'wp_footer', 'dynamic_cache_test_template_tag' );
    	}
    	add_cacheaction( 'add_cacheaction', 'dynamic_cache_test_init' );
    }
    
    /*
     * EXAMPLE 2
     *
     * This is going to be complicated. Hang on!
     *
     * When the cache file for a new page is generated the plugin uses an output
     * buffer to capture the page. A callback function processes the buffer and
     * writes to the cache file. The placeholder tag for any dynamic content has
     * to be written to that cache file but also, it has to be replaced with
     * dynamic content before the page is shown to the user.
     * More on output buffers here: https://php.net/ob_start
     *
     * Unfortunately an extra output buffer is often required when capturing dynamic
     * content such as sidebar widgets. Due to a quirk of the way PHP works it's
     * not possible to have an output buffer run in an output buffer callback. That
     * dynamic content has to be generated before the callback function is reached.
     * The following error occurs when an output buffer is created in the
     * callback function of another output buffer:
     * "PHP Fatal error:  ob_start(): Cannot use output buffering in output buffering display handlers in..."
     *
     * In this example the function add_action() isn't available when this file is
     * loaded so dynamic_output_buffer_init() is hooked on to the "add_cacheaction"
     * cacheaction. That function then hooks dynamic_output_buffer_test() on to the
     * familiar wp_footer action.
     *
     * The first time dynamic_output_buffer_test() runs it generates the dynamic
     * content and captures it with ob_start() in the DYNAMIC_OB_TEXT constant.
     *
     * When the main WP Super Cache output buffer is ready the callback is called.
     * This fires the wpsc_cachedata_safety filter. If the DYNAMIC_OB_TEXT constant
     * is set, which means dynamic content is ready, then it returns 1, a signal
     * that everything is ok.
     * Finally, the wpsc_cachedata filter is run. The function
     * dynamic_output_buffer_test() is hooked on to it. Since DYNAMIC_OB_TEXT is
     * set it replaces the placeholder text with that constant.
     * The resulting html is then sent to the browser.
     *
     * Already cached pages call the safety filter, and then the wpsc_cachedata
     * filter so any hooked function must be ready to generate dynamic content. The
     * very last line of dynamic_output_buffer_test() replaces the placeholder tag
     * with the dynamic content in the cache file.
     *
     *
     * Use an output buffer to capture dynamic content while the page is generated
     * and insert into the right place:
     * Remember to add the DYNAMIC_OUTPUT_BUFFER_TAG text (as defined below) to
     * your theme where the dynamic content should be.
     *
     * dynamic_output_buffer_test() is a function that uses the wpsc_cachedata
     * filter to add a small message and the current server time to every web
     * page. The time increments on every reload.
     *
     */
    
    define( 'DYNAMIC_OUTPUT_BUFFER_TAG', '' ); // Change this to a secret placeholder tag
    
    if ( DYNAMIC_OUTPUT_BUFFER_TAG != '' ) {
    	function dynamic_output_buffer_test( &$cachedata = 0 ) {
    		if ( defined( 'DYNAMIC_OB_TEXT' ) )
    			return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, DYNAMIC_OB_TEXT, $cachedata );
    
    		ob_start();
    		// call the sidebar function, do something dynamic
    		echo "<p>This is a test. The current time on the server is: " . date( 'H:i:s' ) . "</p>";
    		$text = ob_get_contents();
    		ob_end_clean();
    
    		if ( $cachedata === 0 ) { // called directly from the theme so store the output
    			define( 'DYNAMIC_OB_TEXT', $text );
    		} else // called via the wpsc_cachedata filter. We only get here in cached pages in wp-cache-phase1.php
    			return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, $text, $cachedata );
    
    	}
    	add_cacheaction( 'wpsc_cachedata', 'dynamic_output_buffer_test' );
    
    	function dynamic_output_buffer_init() {
    		add_action( 'wp_footer', 'dynamic_output_buffer_test' );
    	}
    	add_cacheaction( 'add_cacheaction', 'dynamic_output_buffer_init' );
    
    	function dynamic_output_buffer_test_safety( $safety ) {
    		if ( defined( 'DYNAMIC_OB_TEXT' ) ) // this is set when you call dynamic_output_buffer_test() from the theme
    			return 1; // ready to replace tag with dynamic content.
    		else
    			return 0; // tag cannot be replaced.
    	}
    	add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' );
    }
    ?>
    

    This is what I get
    https://prntscr.com/hmfrt6

    As you can see without errors, dynamic-cache doesn’t work?

Viewing 15 replies - 1 through 15 (of 18 total)