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

2 Answers 2

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

4
  • Thanks. Been reading about custom posts in the dev docs and since learndash indeed uses custom posts maybe I use the WP_post method? That is, if a plugin does seem to adhere to some WP paradigm, maybe WP core functions then can be used for the fetching the plugin's data? I really wish Learndash had better tech support. They tend to just tell you to hire a developer when you ask dev questions and I have yet to find any technical documentation.
    – Brian
    Commented Oct 14, 2019 at 17:11
  • 1
    WP_Post is just a representation of the post in the database, it has nothing to do with how a plugin stores its models, or if it has models. As for Learndash support, if it's rubbish well.. it's rubbish, that doesn't mean you can ask for Learndash support here. Any data LD stores in posts etc, you can use WP API functions to retrieve, but if you want to grab data from variables and objects LearnDash creates internally, you'd have to check LearnDash docs. Otherwise I think you're trying to learn how to use a framework that simply does not exist. WP provides no supporting controllers or models
    – Tom J Nowell
    Commented Oct 14, 2019 at 17:58
  • I just need the post data at this point. So WP_Query for now is getting what I need. Since I am calling it from a point when user permissions and other plugin specific stuff is verified, I am just going to ignore plugin specifics (for now!), but that said the plugin for the most part seems to integrate into other aspects of the wordpress way of doing things like with taxonomies, tags, metadata. "that doesn't mean you can ask for Learndash support here" Ok, so then where on stackexchange should one ask about wordpress related development topics? I guess I just figured this one was logical...
    – Brian
    Commented Oct 14, 2019 at 18:12
  • 1
    The point is it isn't a WP question, it's a Learndash question, not every question fit neatly on to a stack. You might be able to ask it on stackoverflow. This Q is on topic as it's a general how to get data out of plugins Q, but the Learn Dash specifics would be offtopic, as it specifically requires LearnDash expertise on how it stores and organises its data
    – Tom J Nowell
    Commented Oct 14, 2019 at 19:23
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.