Using WordPress 3.7.1 and PHP 5.4.12 I am trying to add a Meta box - text field to my Custom Post Type. My custom Post Type name is "news" and this is my code:
<?php
/* Custom Meta Boxex */
add_action('add_meta_boxes', 'my_cmbox_add');
add_action('save_post', 'save_options');
function my_cmbox_add()
{
add_meta_box(
"prodInfo-meta",
"News Source ",
"news_source",
"news",
"normal",
"low"
);
}
function news_source()
{
global $post;
$custom = get_post_custom($post->ID);
$source = $custom['source'][0];
?>
<table>
<tr>
<td><?php echo '<label>News Source :</label>'; ?></td>
<td><?php echo '<input name="source" value="'. $source . '" style="width:250px;" />'; ?></td>
</tr>
</table>
<?php
}
function save_options()
{
global $post;
if (!isset($_POST['source']) || $post->post_type != 'news')
{
return $post;
}
update_post_meta($post->ID, "source", $_POST['source']);
}
I am not getting any error but as I said nothing show up in the page. Can you please let me know what I am doing wrong here?
Try to pass parameters .
function my_cmbox_add()( $post_type, $post );
You can also try and use add_meta_boxes_{post_type} for best practice.
Related
I am creating a plugin that creates custom fields on the author profile page.
-It creates custom fields on the user profile page
-We can save it for every user.
What is happening now:
-Creates field and it can be seen in the front end of the author profile page
-On saving the fields do not save.
Observation: I saw the custom meta key and data is not there is the user meta table. So I am thinking we are able to see fields in the front end but database meta key values are not created.
The same code works perfectly when added to functions.php. Please let me if I am missing anything or if any other hooks need to be fired when adding as a plugin.
<?php
class authorFieldsPlugin {
function __construct() {
//Uses author.php from plugin of not there in theme.
add_filter( 'template_include', 'wpa_155871_template_loader' );
function wpa_155871_template_loader( $template ) {
$file = '';
if ( is_author() ) {
$file = 'author.php'; // the name of your custom template
$find[] = $file;
$find[] = 'plugin-name/' . $file; // name of folder it could be in, in user's theme
}
if ( $file ) {
$template = locate_template( array_unique( $find ) );
if ( ! $template ) {
// if not found in theme, will use your plugin version
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/' . $file;
}
}
return $template;
}
function memberpage_rewrite() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init','memberpage_rewrite');
//Custom profile fields
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Author Information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="author"><?php _e("Author Information"); ?></label></th>
<td>
<textarea name="author" id="author" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author', $user->ID ) ); ?></textarea><br />
<span class="description"><?php _e("Please enter Author's Information from plugin."); ?></span>
</td>
</tr>
<tr>
<th><label for="author_title"><?php _e("Author Title"); ?></label></th>
<td>
<textarea name="author_title" id="author_title" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author_title', $user->ID ) ); ?></textarea><br />
<span class="author_title"><?php _e("Please enter author_title."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'author', $_POST['author'] );
}
}
}
$authorFieldsPlugin = new authorFieldsPlugin(); ?>
i want to show a string in the checkout in woocommerce which depends on the shipping country. I use the following code:
<?php global $woocommerce; ?>
<?php $current_cc = $woocommerce->customer->get_shipping_country() ?>
<?php if ($current_cc = DE) { ?>
<p><?php echo var_dump($current_cc);?></p>
<tr class="order-total">
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?><br><small
class="shopping_cart_total_vat_message">inkl. MwSt.</small></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
<?php } else { ?>
<tr class="order-total">
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?><br><small
class="shopping_cart_total_vat_message">exkl. MwSt.</small></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
<?php } ?>
<?php do_action( 'woocommerce_review_order_after_order_total' ); ?>
The var_dump always shows DE also when i choose another country. Where is the problem?
i hope somebody can help.
Thanks
PHP is a server-side language. If the page is not reloaded, the content processed by PHP will not be displayed.
You can get what you want by using a JQuery script.
The following code will only be shown on the checkout page and will display custom text after the shipping country paragraph.
// adds custom text based on shipping country value in checkout
add_action( 'wp_footer', 'add_custom_text_based_on_shipping_country' );
function add_custom_text_based_on_shipping_country() {
// only in the checkout
if ( ! is_checkout() ) {
return;
}
?>
<script type="text/javascript">
// show custom text on page load (based on shipping country value)
jQuery('<div id="custom_string"><span>'+jQuery("#shipping_country").find("option:selected").val()+'</span></div>').insertAfter('#shipping_country_field');
// replaces the text of the #custom_string element based on the value of the selected shipping country
jQuery('#shipping_country').change(function() {
var shippingCountry = jQuery(this).find('option:selected').val();
var customContent = '';
switch ( shippingCountry ) {
case 'FR':
customContent = '<span>'+shippingCountry+'</span>';
break;
case 'DE':
customContent = '<span>'+shippingCountry+'</span>';
break;
case 'IT':
customContent = '<span>'+shippingCountry+'</span>';
break;
default:
customContent = '';
break;
}
jQuery('#custom_string').html(customContent);
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.
I want to build a custom php page that shown registered user from wp,the data saved from wp is a serialized data and i got problem to display. btw i am using contact form 7 database plugin.
i keep getting explode() expect parameter to be string and getting error on line 10.
<?php
//connect database
$conn=new mysqli("localhost","root","","testsaja2");
//call a field from table
$sql= "select form_value from wp_db7_forms";
$result=mysqli_query($conn,$sql);
while($string=mysqli_fetch_array($result));
$explode = explode(PHP_EOL, $string);
foreach ($explode as $line) {
?>
<?php
foreach (unserialize($line) as $item => $value){
?>
<tr>
<td><b><?php echo $item . ": ";?></b></td>
<td><?php if ($value == 'email'){
foreach ($value as $data) {
echo $data ;
}
}
else {
echo $value;
}
?> </td>
</tr>
<br>
<?php } ?>
</table>
<?php } ?>
Modify your code to this.
<?php
//connect database
$conn=new mysqli("localhost","root","","testsaja2");
//call a field from table
$sql= "select form_value from wp_db7_forms";
$result=mysqli_query($conn,$sql);
while($string=mysqli_fetch_array($result)){
$unserialize = unserialize($string['form_value']);
foreach ($unserialize as $item => $value){
?>
<tr>
<td><b><?php echo $item . ": ";?></b></td>
<td><?php echo $value; ?> </td>
</tr>
<?php
} }?>
</table>
I have some meta values on my wordpress custom page that I need to be displayed in a table. Is there any way I can do that.
Here's the code I'm using now: <?php the_meta(); ?>
And this is what it shows:
I want to do something like:
I found out that:
the_meta() is located in wp-includes/post-template.php
This makes the output:
<ul class='post-meta'>
<li><span class='post-meta-key'>your_key:</span> your_value</li>
</ul>
So it's not recommended to edit files in that folder because of the wordpress update.
<table>
<tr><td colspan="2">Game Summary</td></tr>
<?php
$meta = get_post_meta( get_the_ID() );
$exclude = array('_edit_last', '_wp_page_template', '_edit_lock');
foreach( $meta as $key => $value ) {
if( in_array( $key, $exclude) )
continue;
?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value[0]; ?></td>
</tr>
<?php
}
?>
</table>
I am building a custom biography field in the wordpress user profile page using WP's wp_editor api (using WP version 3.3.1).
When I add any content though, the WSYIWYG editor is not writing the paragraph tags to the database.
I am able to use the visual editor to add all other mark-up (bold, italics, etc) & I can manually add paragraph tags and save the full marked-up text to the database.
Has anyone else run into this issue? Code I'm using to build the custom field in the functions.php file below.
//ADD EXTRA FIELDS TO USER PROFILE
add_action( 'show_user_profile', 'extra_profile_fields' );
add_action( 'edit_user_profile', 'extra_profile_fields' );
function extra_profile_fields( $user ) {
//CHECK FOR AUTHOR BIO CONTENT
$check_for_bio = get_the_author_meta('authorbio', $user->ID);
$author_bio = '';
if (!empty($check_for_bio)) { $author_bio = $check_for_bio; }
//BUILD THE INPUT
echo '<table class="form-table">';
echo '<tr>';
echo '<th><label for="authorbio">Author Biography</label></th>';
echo '<td>';
$settings = array('wpautop' => true, 'media_buttons' => false);
wp_editor( $author_bio, 'authorbio', $settings);
echo '</td>';
echo '</tr>';
echo '</table>';
}
//SAVE EXTRA FIELDS TO USER PROFILE
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( $user_id, 'authorbio', $_POST['authorbio'] );
}
Solved the issue. Was not adding the_content fitler. When printing the output.
<?php
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
?>
http://codex.wordpress.org/Function_Reference/the_content
For PHP dummies like I am, this is a possible way how to create an ouput:
<?php
$bio_czech = get_the_author_meta('bio_czech',$user->ID);
echo wpautop($bio_czech);
?>