Last active
December 17, 2015 21:49
-
-
Save rodica-andronache/5677318 to your computer and use it in GitHub Desktop.
WORDPRESS - Metabox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Metabox - sunt casutele din dreapta de la wp-admin, de la adaugare post. | |
- putem adauga unele noi, in plus fata de cele existente. | |
<?php | |
add_meta_box( $id, $title, $callback, $post_type, $context, | |
$priority, $callback_args ); | |
?> | |
EXEMPLU: | |
------------- | |
In functions.php: | |
add_action('admin_menu', 'cwp_post_options_box'); | |
function cwp_post_options_box() { | |
add_meta_box('post_info', 'Testimonial section', 'custom_post_info', 'page', 'side', 'high'); | |
add_meta_box('post_info', 'Testimonial section', 'custom_post_info', 'post', 'side', 'high'); | |
} | |
//Adds the actual option box | |
function custom_post_info() { | |
global $post; | |
?> | |
<fieldset id="mycustom-div"> | |
<div> | |
<p> | |
<label for="cpi_dropdown_options" >Show or hide testimonial on this page/post :</label><br /> | |
<select name="cpi_dropdown_options" id="cpi_dropdown_options"> | |
<option <?php selected( get_post_meta($post->ID, 'cpi_dropdown_options', true), 'Show' ); ?>>Show</option> | |
<option <?php selected( get_post_meta($post->ID, 'cpi_dropdown_options', true), 'Hide' ); ?>>Hide</option> | |
</select> | |
<br /> | |
<br /> | |
<label for="cpi_title_option">Testimonial title:</label><br /> | |
<input type="text" name="cpi_title_option" id="cpi_title_option" value="<?php echo get_post_meta($post->ID, 'cpi_title_option', true); ?>"> | |
<br /> | |
<label for="cpi_text_option">Testimonial text:</label><br /> | |
<input type="text" name="cpi_text_option" id="cpi_text_option" value="<?php echo get_post_meta($post->ID, 'cpi_text_option', true); ?>"> | |
<br /> | |
<label for="cpi_author_option">Testimonial author:</label><br /> | |
<input type="text" name="cpi_author_option" id="cpi_author_option" value="<?php echo get_post_meta($post->ID, 'cpi_author_option', true); ?>"> | |
<br /> | |
<label for="cpi_info_option">Testimonial author details:</label><br /> | |
<input type="text" name="cpi_info_option" id="cpi_info_option" value="<?php echo get_post_meta($post->ID, 'cpi_info_option', true); ?>"> | |
<br /> | |
<br /> | |
<label for="cpi_editor_option"><?php _e('How we helped her','cwp'); ?></label><br /> | |
<?php wp_editor( get_post_meta($post->ID, 'cpi_editor_option', true), 'cpi_editor_option' ); ?> | |
</p> | |
</div> | |
</fieldset> | |
<?php | |
} | |
add_action('save_post', 'custom_add_save'); | |
function custom_add_save($postID){ | |
// called after a post or page is saved | |
if($parent_id = wp_is_post_revision($postID)) | |
{ | |
$postID = $parent_id; | |
} | |
if (isset($_POST['cpi_dropdown_options'])) { | |
update_custom_meta($postID, $_POST['cpi_dropdown_options'], 'cpi_dropdown_options'); | |
} | |
if (isset($_POST['cpi_text_option'])) { | |
update_custom_meta($postID, $_POST['cpi_text_option'], 'cpi_text_option'); | |
} | |
if (isset($_POST['cpi_title_option'])) { | |
update_custom_meta($postID, $_POST['cpi_title_option'], 'cpi_title_option'); | |
} | |
if (isset($_POST['cpi_author_option'])) { | |
update_custom_meta($postID, $_POST['cpi_author_option'], 'cpi_author_option'); | |
} | |
if (isset($_POST['cpi_info_option'])) { | |
update_custom_meta($postID, $_POST['cpi_info_option'], 'cpi_info_option'); | |
} | |
if (isset($_POST['cpi_editor_option'])) { | |
update_custom_meta($postID, $_POST['cpi_editor_option'], 'cpi_editor_option'); | |
} | |
} | |
function update_custom_meta($postID, $newvalue, $field_name) { | |
// To create new meta | |
if(!get_post_meta($postID, $field_name)){ | |
add_post_meta($postID, $field_name, $newvalue); | |
}else{ | |
// or to update existing meta | |
update_post_meta($postID, $field_name, $newvalue); | |
} | |
} | |
Unde vreau sa afisez, sa zic in page.php: | |
<?php | |
$show = get_post_meta($id, 'cpi_dropdown_options'); | |
$text = get_post_meta($id, 'cpi_text_option'); | |
$title = get_post_meta($id, 'cpi_title_option'); | |
$author = get_post_meta($id, 'cpi_author_option'); | |
$info = get_post_meta($id, 'cpi_info_option'); | |
if((isset($show[0]) && $show[0] == 'Show') || !isset($show[0])): | |
?> | |
<section class="testimonials"> | |
<div class="bg-texture"></div> | |
<div class="container"> | |
<div class="content"> | |
<?php if(isset($title[0]) && $title[0] != ''): ?> | |
<h2> | |
<?php echo $title[0]; ?> | |
</h2> | |
<?php endif; ?> | |
<div class="testimonial-box"> | |
<?php if(isset($text[0]) && $text[0] != ''): ?> | |
<p class="text"> | |
<?php echo $text[0]; ?> | |
</p> | |
<?php endif; ?> | |
<p class="client-info"><a><?php if(isset($author[0]) && $author[0] != '') echo $author[0].' '; ?><span><?php if(isset($info[0]) && $info[0] != '') echo $info[0].' '; ?></span></a></p> | |
</div><!-- .testimonial-box --> | |
</div><!-- .content --> | |
</div><!-- .container --> | |
</section><!-- .testimonials --> | |
<?php | |
endif; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment