2

I'm entirely new to OOP, but trying to dip my toe in by creating a simple Recipes plugin. I have added a Recipes custom post type and a few meta fields to go along with it, and now I am trying to create a few template files for displaying the recipe meta and content. To do this, I am thinking it would be useful to create a class to get all the meta for a particular post.

To test, I created a template file that is meant to echo one sentence with a single meta value after the post content:

$recipe = new Wp_Recipes_Recipe;
echo '<p>The prep time for this recipe is ' . $recipe->$recipeprep . '</p>';

And I created a new file in plugin-dir > public called "class-wp-recipes-recipe.php" that contains the following:

class Wp_Recipes_Recipe {

  public function __construct( $post_id ) {

    $this->$recipemeta = get_post_custom($post_id);
    $this->$recipeprep = $this->$recipemeta['_rcp-prep-time'][0];

  }

}

I think I need to add some code to specify to include my new file, but I am just not sure where to put it. I have tried putting a "require_once" for the file in the load_dependencies() function in the includes folder. No matter what I try, the meta value I am trying to display does not display, and the only noticeable effect from my efforts is that the wp admin bar no longer displays.

I may be going about this completely the wrong way, but any guidance would be greatly appreciated! Thanks!

2 Answers 2

1

Your constructor requires the post ID to be passed to the class when it's instatiated, try this

$recipe = new Wp_Recipes_Recipe( get_the_ID() );

2
  • While this is true, it does not address the actual question, which was about including class files. Commented Apr 18, 2020 at 4:42
  • I marked this as the accepted answer, as it was the source of the issue I was experiencing. The answer by Jacob is a more direct answer to the question I asked, but I had actually tried it already - just my own inexperience with OOP led me to assume I wasn't using 'require_once' correctly. Commented Apr 18, 2020 at 15:04
3

When WordPress loads your plugin it only automatically loads the main plugin file. The one with this at the top:

<?php
/**
 * Plugin Name: YOUR PLUGIN NAME
 */

If you have function or class definitions in other files that you want to use, then you need to include them into this file. If your class file is public/class-wp-recipes-recipe.php, then you would include it like this, using plugin_dir_path():

<?php
/**
 * Plugin Name: YOUR PLUGIN NAME
 */

require_once plugin_dir_path( __FILE__ ) . 'public/class-wp-recipes-recipe.php';

Now your class will be available to use anywhere in WordPress after your plugin has loaded.

You might want to put your includes into a function, only include it when needed, or even experiment with an autoloader, but this is the minimum required to load PHP files into a plugin.

Lastly, you should not use the Wp_ prefix for your own classes and functions. The purpose of a prefix is to namespace them to avoid conflicts with other themes, plugins, and WordPress itself. While it's highly unlikely WordPress will ever include a class named Wp_Recipes_Recipe, you should treat Wp_ as reserved and use your own unique prefix (or even namespaces, now that WordPress requires PHP 5.6).

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.