Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/4f91608bebcc8feb0008 to your computer and use it in GitHub Desktop.
Save igorbenic/4f91608bebcc8feb0008 to your computer and use it in GitHub Desktop.
Ponavljajuća Polja za Metabox
<?php
/*
* Plugin Name: Ponavljajući Meta Field
* Plugin URI: www.lakotuts.com
* Description: Dodatak u koji dodaje ponavljajuće polje.
* Version: 1.0.0
* Author: Igor Benić
* Author URI: www.twitter.com/igorbenic
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
class ponav_metabox {
private $post_types = array('post','page');
/**
* Pri inicijalizaciji objekta, hookaj mi dvije metoda unutar ove klase
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
}
/**
* Adds the meta box container.
*/
public function add_meta_box( $post_type ) {
$post_types = $this->post_types; //Ograniči samo na određene post type-ove
if ( in_array( $post_type, $post_types )) {
add_meta_box(
'ponav_metabox'
,__( 'Metabox sa ponavljajućim poljem', 'lakotuts' )
,array( $this, 'render_meta_box_content' )
,$post_type
,'advanced'
,'high'
);
}
}
public function save($post_id){
/*Ovdje ide kod za spremanje*/
}
public function render_meta_box_content( $post ) {
/* Ovdje ide kod za ispis meta */
}
/*END CLASS*/
}
....
private $post_types = array('post','page');
private $name = "ponav";
....
public function save( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST[$this->name.'_nonce'] ) )
return $post_id;
$nonce = $_POST[$this->name.'_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, $this->name."_secure" ) )
return $post_id;
// If this is an autosave, our form has not been submitted,
// so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
/* OK, its safe for us to save the data now. */
$values = $_POST[$this->name];
// Update the meta field.
update_post_meta( $post_id, '_'.$this->name, $values );
}
public function render_meta_box_content( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( $this->name."_secure", $this->name.'_nonce' );
// Use get_post_meta to retrieve an existing value from the database.
$values = get_post_meta( $post->ID, '_'.$this->name, true );
$max = 0;
// Display the form, using the current value.
echo "<table>";
echo "<tr><td colspan='2'><h2>".__("Polja","lakotuts")."</h2></td></tr>";
if(!empty($values)){
foreach ($values as $key => $value) {
echo "<tr>";
echo "<td><input type='text' value='".$value["type"]."' name='".$this->name."[".$key."][type]' /></td>";
echo "<td><input type='text' value='".$value["value"]."' name='".$this->name."[".$key."][value]' /><a class='button button-small' id='clearField'>Obriši polje</a></td>";
echo "</tr>";
if($key > $max){
$max = $key;
}
}
}
echo "<tr><td colspan='2'><a class='button' data-count='".$max."' id='addField'>Dodaj novo polje</a></td></tr>";
echo "</table>";
}
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
public function enqueue_scripts(){
wp_enqueue_script( $this->name."-js", plugin_dir_url( __FILE__ ) . 'js/ponav.js', array( 'jquery' ) );
}
function ponav_createMetabox(){
$metabox = new ponav_metabox();
}
add_action( 'load-post.php', 'ponav_createMetabox');
add_action( 'load-post-new.php', 'ponav_createMetabox');
(function( $ ) {
'use strict';
$(function() {
});
})( jQuery );
jQuery("#addField").click(function(){
var $counter = $(this).attr("data-count");
$counter = parseInt($counter);
$counter = $counter + 1;
var $row = "<tr><td>";
$row += "<input type='text' name='ponav["+$counter+"][type]' /></td>";
$row += "<td><input type='text' name='ponav["+$counter+"][value]' /></td> </tr>";
jQuery(this).parent("td").parent("tr").after($row);
$(this).attr("data-count",$counter);
});
jQuery("#clearField").click(function(){
jQuery(this).parent("td").parent("tr").remove();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment