C
C
coderisimo2015-11-14 00:55:37
WordPress
coderisimo, 2015-11-14 00:55:37

WordPress plugin with CUSTOM POST TYPE. Does it make sense?

Alas, a complete amateur in this area. In this regard, I ask for advice from more sophisticated colleagues)) I
usually create plugins according to the following scheme. I am writing the plugin file itself, where I attach the template connection, the necessary scripts and styles to the shortcode. If any parameters are needed, I pass them through a short code.
affairs. it's all like this:

function load_table_manager()
    {
        wp_enqueue_style('table_manager_css',"//handsontable.com/dist/handsontable.full.css");      
        ob_start();
        include dirname(__FILE__) . '/template.php'; // здесь вся нужная разметка
        $result = ob_get_contents();
        ob_end_clean();
        table_manager_scripts(); // функция для подгрузки JS через wp_enqueue_script 
        return $result;

    }

 add_shortcode('table_manager','load_table_manager');

// further functions for ajax operation, etc.
By the way, if this method seems barbaric to you, tell me why. What I like about this approach. The plugin is completely standalone. Everything he needs is stored in his folders.
And now the question. You need to implement a spreadsheet editor. That is, a table is created on the admin side, edited and inserted for demonstration to visitors. For tables, Handsontable is used (requirement of the customer). Quite a handy tool, which even I (!!) sort of figured out.)))) The customer wants to implement it with the creation of a custom post type. I don't understand the benefits. Correct me if I'm wrong. I will have to edit the routing, I will have to write not only in the plugin file, but also in the theme templates. That is, the autonomy of the plugin will go to .... in general, not at all there. I don't see any advantages, most likely due to a lack of in-depth knowledge of the subject.
Now in the admin panel we create / edit a new table. Add a shortcode like this to any page:[table_manager table_name ='my_best_table']and that's it! Why is CUSTOM POST TYPE here? What is the profit besides the minus in the form of a loss of plugin autonomy?
Thanks
UPDATE/ perhaps templates can be overridden in the plugin itself, like this:
function get_custom_post_type_template($single_template) {
     global $post;

     if ($post->post_type == 'data_table') {
          $single_template = dirname( __FILE__ ) . '/template.php';
     }
     return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_template' );

this part of the problem is solved, but what about routing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2015-11-15
@HeadOnFire

Regarding the need to go beyond the standalone plugin - this is not necessary, CPT can be non-public and available only in the admin + through your plugin in your template, without routing, etc.
As for everything else - yes, you are doing it very inefficiently, and, I would even say crookedly. No offense. Now I’m already lying down to rest, tomorrow I’ll try to find a minute and write in detail what is wrong and where to dig (I will update this answer).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question