Code Monkey home page Code Monkey logo

custom-field's Introduction

miya/custom-field

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

An abstract class to create a custom field for WordPress.

Install

$ composer require miya/custom-field

Example

<?php

require_once dirname( __FILE__ ) . '/vendor/autoload.php';

$text_field = new Text_Field( 'text', 'Text' );
$text_field->add( 'post' );


class Text_Field extends \Miya\WP\Custom_Field
{
	/**
	 * Fires at the `admin_enqueue_scripts` hook.
	 *
	 * @param string $hook The hook like `post.php` or so.
	 * @return none
	 */
	public function admin_enqueue_scripts( $hook )
	{
		// If you need a CCS or JS, you can run `wp_enqueue_*()` here.
		// This function will be callded when current screen is the specific post type by `add()`.
	}

	/**
	 * Displays the form for the metabox. The nonce will be added automatically.
	 *
	 * @param object $post The object of the post.
	 * @param array $args The argumets passed from `add_meta_box()`.
	 * @return none
	 */
	public function form( $post, $args )
	{
		?>
			<input type="text" name="input"
					value="<?php echo esc_attr( get_post_meta( get_the_ID(), '_input', true ) ); ?>">
		<?php
	}

	/**
	 * Save the metadata from the `form()`. The nonce will be verified automatically.
	 *
	 * @param int $post_id The ID of the post.
	 * @return none
	 */
	public function save( $post_id )
	{
		update_post_meta( $post_id, '_input', $_POST['input'] );
	}
}

The result is following.

custom-field's People

Contributors

miya0001 avatar

Stargazers

Bowo avatar

Watchers

 avatar James Cloos 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.