最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is wordpress way to access a plugin's classesmodels to load custom post type data?

programmeradmin0浏览0评论

New to Wordpress development.

I am using a plugin that uses custom posts types to create online courses.

I noticed that the plugin has a file called

/wp-content/plugins/sfwd-lms/includes/classes/class-ldlms-topic-model.php

It contains:

<?php
if ( ( class_exists( 'LDLMS_Model_Post' ) ) && ( ! class_exists( 'LDLMS_Model_Topic' ) ) ) {
    class LDLMS_Model_Topic extends LDLMS_Model_Post {

        private static $post_type = 'sfwd-topic';

        function __construct( $topic_id = 0 ) {
            $this->load( $topic_id );
        }

        // Endof functions.
    }
}

So in a hook action I am trying to access the plugin's models. For instance as so:

require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/classes/abstract-ldlms-model-post.php';
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/classes/class-ldlms-topic-model.php';

add_action( 'learndash-topic-quiz-row-before', 'show_topic_content', 10, 3);
function show_topic_content($topicID, $course_id, $user_id) {
    // get model
    $thistopic = new LDLMS_Model_Topic($topicID);
    print_r($thistopic);die;
}

But I get this error:

Fatal error: Uncaught Error: Call to undefined method LDLMS_Model_Topic::load() in wp-content/plugins/sfwd-lms/includes/classes/class-ldlms-topic-model.php:8 

I just came from Laravel development so I suppose there is a wordpress-ish way to go about accessing a model and maybe I am missing some step?

Or maybe, because a topic in this plugin is a type of post I need to use the post model? Seems like that would be odd since this topic model is its own unique sort of post.

Would be very curious to know what is the proper, plugin-agnostic way to access a model, its permissions.

Thanks, Brian

New to Wordpress development.

I am using a plugin that uses custom posts types to create online courses.

I noticed that the plugin has a file called

/wp-content/plugins/sfwd-lms/includes/classes/class-ldlms-topic-model.php

It contains:

<?php
if ( ( class_exists( 'LDLMS_Model_Post' ) ) && ( ! class_exists( 'LDLMS_Model_Topic' ) ) ) {
    class LDLMS_Model_Topic extends LDLMS_Model_Post {

        private static $post_type = 'sfwd-topic';

        function __construct( $topic_id = 0 ) {
            $this->load( $topic_id );
        }

        // Endof functions.
    }
}

So in a hook action I am trying to access the plugin's models. For instance as so:

require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/classes/abstract-ldlms-model-post.php';
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/classes/class-ldlms-topic-model.php';

add_action( 'learndash-topic-quiz-row-before', 'show_topic_content', 10, 3);
function show_topic_content($topicID, $course_id, $user_id) {
    // get model
    $thistopic = new LDLMS_Model_Topic($topicID);
    print_r($thistopic);die;
}

But I get this error:

Fatal error: Uncaught Error: Call to undefined method LDLMS_Model_Topic::load() in wp-content/plugins/sfwd-lms/includes/classes/class-ldlms-topic-model.php:8 

I just came from Laravel development so I suppose there is a wordpress-ish way to go about accessing a model and maybe I am missing some step?

Or maybe, because a topic in this plugin is a type of post I need to use the post model? Seems like that would be odd since this topic model is its own unique sort of post.

Would be very curious to know what is the proper, plugin-agnostic way to access a model, its permissions.

Thanks, Brian

Share Improve this question edited Oct 14, 2019 at 17:38 Brian asked Oct 14, 2019 at 16:15 BrianBrian 3372 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

I just came from Laravel development so I suppose there is a wordpress-ish way to go about accessing a model and maybe I am missing some step?

You don't, Laravel is a framework that provides structure, and so models are models, but in WP there's none of that.

Plugins in WordPress are just PHP files with a comment at the top with some meta data. They're not contained, sandboxed, or separated in any way, which means:

  • you can't infer anything from the folder/file name a class might be in
  • those objects and variables aren't stored in a common place, unless that plugin puts it in one
  • there is no standard way to grab a plugins data structures, if it has any data structures at all

A plugin may not have any models to fetch, it may use a different programming paradigm, it may not store any data at all. A plugin might even load parts of Laravel and use Laravel to structure itself internally. I've used Symfony components in WP plugins myself

So if the data is stored using WordPress APIs such as custom post types, post meta, taxonomies, etc, then you can use all the standard WP functions to retrieve them. You will need to do some investigation to understand what the post meta values LearnDash stores, and what they mean and do though.

As for controllers and models, WP provides no controllers and models. Any classes WP does provide are not places to store or extend. It isn't an OO framework, or a framework at all, you have to provide those bits.

So how do you get that object with the data you want?

  • Look at the learndash docs
  • Read the code to figure out where that class is instantiated into an object and how it's stored
  • Look for singletons, static variables, global variables, filters
  • Accept that there may not be a way to fetch it

But more importantly, contact LearnDash support. 3rd party plugins are offtopic, so any answer you get here will be generic, it won't tell you the specifics of LearnDash. If LearnDash support can't help, then that's unfortunate, you'll need to hire a developer who knows LearnDash, investigate alternatives, or read the LearnDash code and docs to figure it out

Ok, figured it out. The key is that the plugin I am using registers its posts as custom post types. So the 2nd sentence of my post had the key all along: "I am using a plugin that uses custom posts types" :-)

According to Wordpress Plugin Dev Guide

You can query posts of a specific type by passing the post_type key in the arguments array of the WP_Query class constructor.

Based on that, voila, this works:

function show_topic_content($topicID, $course_id, $user_id) {
    // get model
    $args = array(
        'post_type'   => 'sfwd-topic',
        'p' => $topicID
    );
    $query = new WP_Query( $args );
    $post = $query->posts;
    // do stuff
}

Probably basic stuff for pros, but eye opening for me!

thanks, Brian

发布评论

评论列表(0)

  1. 暂无评论