Code Monkey home page Code Monkey logo

easy-wordpress-custom-post-types's Introduction

Usage

Firstly, this class requires PHP 5.3. Make sure you have it running.

Secondly, this is still quite new, so it needs a lot of debugging and work. :) In other words, it's Beta.

First, download the class, and drag it into the root of your theme directory.

Next, within functions.php, require the class.

require 'jw_custom_posts.php';

You now have access to the class and its functions. Instantiate the class. We'll use a Snippet post type as an example.

$snippet = new JW_Post_Type('Snippet');

You may also pass an optional second parameter to override some of the defaults. For example, if I only want to provide support for a title and an excerpt, I could do:

$snippet = new JW_Post_Type('Snippet', array(
   'supports' => array('title', 'excerpt')
);

If I want to also use the built-in category and/or tag taxonomies that WordPress provides...

$snippet = new JW_Post_Type('Snippet', array(
   'taxonomies' => array('category', 'post_tag')
);

Custom Taxonomies

It makes sense to filter our sample Snippet post type by difficulty and language. We can implement that functionality quite easily.

$snippet->add_taxonomy('Difficulty');
$snippet->add_taxonomy('Language');

I may also specify the plural form of my taxonomy, and any optional overrides.

$snippet->add_taxonomy('Difficulty', 'Difficulties', array(
  'show_ui' => false
);

Meta Boxes

Our Snippet post type should allow me to enter additional information about the single snippet - perhaps a GitHub link, additional notes, an associated url for the snippet, etc. These items are unique to each post, and can be displayed in a custom meta box.

$snippet->add_meta_box('Snippet Info', array(
  'Associated URL' => 'text',
  'GitHub Link' => 'text',
  'Additional Notes' => 'textarea',
  'Original Snippet' => 'checkbox',
  'Snippet Image' => 'file'
));

Within the second array argument, set the label text and the type of input to display, respectively. The available input choices are:

  • text
  • textarea
  • checkbox
  • file (for uploading images and files),
  • select (for dropdown)

However, if you require a select box, you need to pass an array, with the first key equaling the type of input to create ('select' element), and the second key being an array of choices. For example:

$snippet->add_meta_box( 'Personal Info', array(
  'Name' => 'text',
  'Bio'  => 'textarea'
  'Favorite Food' => array( 'select', array('pizza', 'tacos') )
);

easy-wordpress-custom-post-types's People

Contributors

jeffreyway avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.