Created
June 7, 2015 15:57
-
-
Save gwin/1611e458489b9810b2d7 to your computer and use it in GitHub Desktop.
WORK IN PROGRESS Explains Adverts Forms API
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
<?php | |
/** | |
* Plugin Name: [Adverts] Custom Fields API | |
* Version: 1.0 | |
* Author: Greg Winiarski | |
* Description: This plugin explains how to create custom fields for Add Advert form. | |
*/ | |
add_filter( "adverts_form_load", "my_adverts_form_load" ); | |
function my_adverts_form_load( $form ) { | |
if( $form["name"] != "advert" ) { | |
return $form; | |
} | |
// remove Price field | |
foreach($form["field"] as $k => $v) { | |
if($v["name"] == "adverts_price") { | |
unset($form["field"][$k]); | |
} | |
} | |
// add new custom field | |
$form["field"][] = array( | |
"name" => "my_custom_field", | |
"type" => "adverts_field_text", | |
"order" => 25, | |
"label" => "Custom Field", | |
"is_required" => true, | |
"validator" => array( | |
array( "name" => "is_required" ), | |
) | |
); | |
return $form; | |
} | |
add_action( "adverts_tpl_single_details", "my_adverts_tpl_single_details" ); | |
// display custom field value in the frontend | |
function my_adverts_tpl_single_details( $post_id ) { | |
$cf = get_post_meta( $post_id, "my_custom_field", true); | |
if( empty( $cf ) ) { | |
return; | |
} | |
?> | |
<div class="adverts-grid-row"> | |
<div class="adverts-grid-col adverts-col-30"> | |
<span class="adverts-round-icon adverts-icon-wordpress"></span> | |
<span class="adverts-row-title">Custom Field</span> | |
</div> | |
<div class="adverts-grid-col adverts-col-65"> | |
<?php esc_html_e( $cf ) ?> | |
</div> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do you have a completed version