Get start date from Event Calendar wordpress - wordpress

I'm working on an event website in Wordpress and I use this plugin: https://theeventscalendar.com/
All the documentation files say that I have to use tribe_get_start_date() to get the event date. So that's what I'm using in my custom loop.
$events = tribe_get_events();
if ( empty( $events ) ) {
echo 'Sorry, nothing found.';
} else foreach ($events as $event) {
echo get_the_title( $event );
echo tribe_get_start_date( null, false, 'j F Y' ):
}
But the function tribe_get_start_date always gives the current date instead of the event date.
What can be the issue?
The ideal situation would be that the loop only shows the 4 upcoming events.

Maybe it is due to you passing null to your tribe_get_start_date function, when you might want to be passing $event.
$events = tribe_get_events();
if ( empty( $events ) ) {
echo 'Sorry, nothing found.';
} else foreach ($events as $event) {
echo get_the_title( $event );
echo tribe_get_start_date( $event, false, 'j F Y' ):
}

Related

Problems with a custom pay link shortcode on WooCommerce/wordpress

Hello I am trying to create custom payment links for various payment methods. I would like the link to be generated on WooCommerce based on a cart or order total at the checkout page. Here is what I have been trying in my functions php file.
function customhaywoo(){
global $woocommerce, $total, $amount ;
$woocommerce->cart->get_cart();
$total = $woocommerce->cart->get_total();
$amount = $woocommerce->cart->total;
if ( ! $amount ) {
return;
}
extract(shortcode_atts(array(
"href" => 'https://venmo.com/hayden-55?txn=pay&amount=',
esc_attr( wp_kses_post( $amount ) ),
), $atts));
return ''.$content.'';
}
add_shortcode ('haywoo','customhaywoo');
I have tried other combinations as well however as you can probably tell I have absolutely no experience and have no idea what I am doing. I got the idea of what I should include in the code from "checkout with Venmo" by the African boss plugin. I would just use the plugin however I need this for a few other Payment methods. For many of the Payment methods you can just add the number amount at the end of the url and it will automatically have the amount inserted into the payment method. Basically I somehow need to get the cart or order total to appear at the end of a link without the currency symbol.
Edit- I tried this for displaying on the thank you page. This just shows up as nothing at all. not even the [haywoo]. Any help on this would be awesome!
function customhaywoo()
{
$order = new WC_Order( $order_id );
$total = $order->get_total();
$order_total = floatval( preg_replace( '#[^\d.]#', '', $order->get_total()) );
if (!$order_total) {
return;
}
ob_start();
$pay_link = 'https://venmo.com/hayden-595?txn=pay&amount='.$order_total;
$payment_text = __('Click here to pay '.$order_total, 'text_domain');
echo '' . $payment_text . '';
$contents = ob_get_contents();
ob_end_clean();
return $contents; ''.$content.'';
}
add_shortcode('haywoo', 'customhaywoo');
If you want the shortcode to output the payment link with the total amount of cart then try out below edited code.
function customhaywoo()
{
global $woocommerce, $total, $amount;
$woocommerce->cart->get_cart();
$total_amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
if (!$total_amount) {
return;
}
ob_start();
$pay_link = 'https://venmo.com/hayden-55?txn=pay&amount='.$total_amount;
$payment_text = __('Click here to pay '.$total_amount, 'text_domain');
echo '' . $payment_text . '';
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
add_shortcode('haywoo', 'customhaywoo');
I Hope it helps.

WooCommerce Hook - woocommerce_after_cart_item_name

I am using a function to add custom meta to products. I have used the following hooks for SHOW on Product Loop woocommerce_after_shop_loop_item and Product Single Page woocommerce_product_meta_end.
So, when applying to get the same results on CART PAGE product/item by using the hook
woocommerce_after_cart_item_name it doesn’t work with no results.
Why isn’t the hook woocommerce_after_cart_item_name working if it works with the other previous hooks mentioned?
This the code I am using. I just change the hook to make it to show in Product Loop and Product Single Page, need it to show on cart Products as well. I was just wondering why it doesn't work with cart item hook..
public function woocommerce_after_cart_item_name()
{
global $product;
if ( !PluginOptions::value('shop_season') && !PluginOptions::value('shop_car_type') )
return;
$product_id = $product->get_id();
$season = get_post_meta( $product_id, 'season', true );
$car_type = get_post_meta( $product_id, 'car_type', true );
$tips_seasons = $this->ui_tips_season();
$tips_car_types = $this->ui_tips_car_types();
?>
<div class="tyre-details tyre-tips">
<?php if ( PluginOptions::value('shop_season') && $season ): ?>
<?php echo $tips_seasons[ strtolower($season) ]; ?>
<?php endif ?>
<?php if ( PluginOptions::value('shop_car_type') && $car_type ): ?>
<?php echo $tips_car_types[ strtolower($car_type) ]; ?>
<?php endif ?>
</div>
<?php
}
It is from a plugin. I was just given this code from woocommerce support but i do not know how to complete it. He says to replace with my meta_keys in the code which I will post below here. Can you help me finish this? or tell me where I need to replace. My meta_keys are $season and $car-type but i don't know how to apply with the code provided by woocommerce.
// Display custom cart item meta data (in cart and checkout)
add_filter( 'woocommerce_get_item_data', 'display_cart_item_custom_meta_data', 10, 2 );
function display_cart_item_custom_meta_data( $item_data, $cart_item ) {
$meta_key = 'PR CODE';
if ( isset($cart_item['add_size']) && isset($cart_item['add_size'] [$meta_key]) ) {
$item_data[] = array(
'key' => $meta_key,
'value' => $cart_item['add_size'][$meta_key],
);
}
return $item_data;
}
// Save cart item custom meta as order item meta data and display it everywhere on orders and email notifications.
add_action( 'woocommerce_checkout_create_order_line_item', 'save_cart_item_custom_meta_as_order_item_meta', 10, 4 );
function save_cart_item_custom_meta_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
$meta_key = 'PR CODE';
if ( isset($values['add_size']) && isset($values['add_size'][$meta_key]) ) {
$item->update_meta_data( $meta_key, $values['add_size'][$meta_key] );
}
}
I located the ClassName (class FeatureTyreTips) and added that to the code you provided. I entered this in functions.php and it said fatal error when loading the cart page. I also tried placing it in the cart.php with same results...and I tried in the same plugin file where this code came from originally. All 3 locations did not work...only difference was that it did not show fatal error when adding and activating your new code in the plugin file when loading cart page. Any ideas? grazie Vdgeatano
The "Fatal Error" is most likely due to the fact that "the non-static method cannot be called statically": PHP - Static methods
I have now corrected the code.
Considering that the class name that was missing in your code is FeatureTyreTips, the correct code to add some text after the product name is:
add_action( 'woocommerce_after_cart_item_name', 'add_custom_text_after_cart_item_name', 10, 2 );
function add_custom_text_after_cart_item_name( $cart_item, $cart_item_key ) {
// create an instance of the "PluginOptions" class
$PluginOptions = new PluginOptions();
if ( ! $PluginOptions->value( 'shop_season' ) && ! $PluginOptions->value( 'shop_car_type' ) ) {
return;
}
$product = $cart_item['data'];
$season = get_post_meta( $product->get_id(), 'season', true );
$car_type = get_post_meta( $product->get_id(), 'car_type', true );
// create an instance of the "FeatureTyreTips" class
$FeatureTyreTips = new FeatureTyreTips();
$tips_seasons = $FeatureTyreTips->ui_tips_season();
$tips_car_types = $FeatureTyreTips->ui_tips_car_types();
if ( ! $PluginOptions->value('shop_season') || ! $season ) {
$season = '';
}
if ( ! $PluginOptions->value('shop_car_type') || ! $car_type ) {
$car_type = '';
}
$html = '<div class="tyre-details tyre-tips">' . trim( $season . ' ' . $car_type ) . '</div>';
echo $html;
}
The code has been tested (where possible) and works.It needs to be added to your theme's functions.php file or in your plugin.

How do you add_filter to only one instance of the_title()

I created a filter that modifies the_title() of a post, but the problem I'm having is that it's modifying every instance of the_title(). I got most of the problem sorted out with the in_the_loop() function, however any theme that has "next post" "previous post" navigation links within the loop are still having the filter applied (understandably so). How can I apply the filter to only the the_title() of the current post?
function xyz_the_title( $the_title ) {
if( !in_the_loop() )
return $the_title;
$location = get_post_meta( get_the_ID(), 'location', true );
$the_title .= ' - ' . $location;
return $the_title;
}
add_filter( 'the_title', 'xyz_the_title' );
do it with jQuery
Something like this should do the trick:
$(document).ready(function() {
$('#entry-header').text(function(i, oldText) {
return oldText === 'Popular Science' ? 'New word' : oldText;
});
});
This only replaces the content when it is Popular Science. See text in the jQuery API.
Rather than filtering the_title you could edit your template file instead and append the location to your returned the_title().
echo "<h1>" . get_the_title() . " - " . $location . "</h1>";
Ran into a similar situation and was hoping this thread could save me...
Anyways this is what I managed to do thus far in
add_filter( 'the_title', function( $title, $id ){
/**
* don't run in the backend
*/
if( is_admin() ) {
return $title;
}
/**
* invalid values received
*/
if( empty( $title ) || $id < 1 ){
return $title;
}
global $post;
if ( ! $post instanceof WP_Post ){
return $title;
}
/**
* PREVENTATIVE MEASURE...
* only apply the filter to the current page's title,
* and not to the other title's on the current page
*/
global $wp_query;
if( $id !== $wp_query->queried_object_id ){
return $title;
}
/**
* Don't run this filter if wp_head calls it
*/
if( doing_action( 'wp_head' ) ){
return $title;
}
return 'MODIFIED - '.$title;
});
Here are the shortfalls:
If you have the same post being displayed on the current page, somewhere else, it's gonna modify THAT post title as well
Currently thinking about having a look at the call stack to detect if the call is coming from the theme...
but I would suggest you find another solution bud...
Ah, didn't know it was for a plugin. In which case, I think you should be ok to use if_filter then. This checks whether the filter has been run x times on the page in question. So, we check if it has run once on the page and if so it won't run again. Also, I have assumed you only want this to run on single post pages. This is untested.
function xyz_the_title( $the_title ) {
if( is_single() AND did_filter('the_title') === 1 ) {
if( !in_the_loop() )
return $the_title;
$location = get_post_meta( get_the_ID(), 'location', true );
$the_title .= ' - ' . $location;
return $the_title;
}
}
add_filter( 'the_title', 'xyz_the_title' );

Move custom column admin links from below "post title"

It seems I can't figure out how to add the edit, remove, view, etc... to one of my custom columns in the backend of wordpress. The idea is to get the links that are attached to title when one hovers over the title, to be attached to a different column.
This is what the below code outputs.
This is what I want the link in the authors column have when mouse is hovered over the authors, just like when you hover over the title in this screenshot; all the edit links.
This is what I have so far:
add_filter( 'manage_edit-testimonial-quotes_columns', 'view_columns' ) ;
function view_columns( $columns ) {
$columns = array(
'cb' => '',
'date' => __( 'Date' ),
'tq_author' => __( 'Author' ),
'tq_quote' => __( 'Testimonial' ),
);
return $columns;
}
add_action('manage_testimonial-quotes_posts_custom_column', 'custom_view_columns', 10, 2);
function custom_view_columns($column, $post_id){
global $post;
switch ($column){
case 'tq_author':
echo '<a href="post.php?post=' . $post->ID . '&action=edit">';
$column_content = the_field('tq_author');
echo $column_content;
echo '</a>';
break;
case 'tq_quote':
$column_content = the_field('tq_quote');
echo $column_content;
break;
default:
break;
}
}
The best way of doing this since WP 4.3.0 is using
add_filter( 'list_table_primary_column', [ $this, 'list_table_primary_column' ], 10, 2 );
public function list_table_primary_column( $default, $screen ) {
if ( 'edit-yourpostype' === $screen ) {
$default = 'yourcolumn';
}
return $default;
}
I really doubt that there's a hook to deal with that. So, I'll not even check the core and go straight to the dirty solution:
add_action( 'admin_head-edit.php', 'so_13418722_move_quick_edit_links' );
function so_13418722_move_quick_edit_links()
{
global $current_screen;
if( 'post' != $current_screen->post_type )
return;
if( current_user_can( 'delete_plugins' ) )
{
?>
<script type="text/javascript">
function so_13418722_doMove()
{
jQuery('td.post-title.page-title.column-title div.row-actions').each(function() {
var $list = jQuery(this);
var $firstChecked = $list.parent().parent().find('td.author.column-author');
if ( !$firstChecked.html() )
return;
$list.appendTo($firstChecked);
});
}
jQuery(document).ready(function ($){
so_13418722_doMove();
});
</script>
<?php
}
}
Result:
Notes:
adjust your post_type: 'post' != $current_screen->post_type
adjust your column classes: find('td.author.column-author')
Bug and solution:
You'll note that, after updating, the quick-edit menu goes back to its original position. The following AJAX interception deals with it. Refer to this WordPress Developers answer for more details.
add_action( 'wp_ajax_inline-save', 'so_13418722_ajax_inline_save' , 0 );
/**
Copy of the function wp_ajax_inline_save()
http://core.trac.wordpress.org/browser/tags/3.4.2/wp-admin/includes/ajax-actions.php#L1315
Only Modification marked at the end of the function with INTERCEPT
*/
function so_13418722_ajax_inline_save()
{
global $wp_list_table;
check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
wp_die();
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_ID ) )
wp_die( __( 'You are not allowed to edit this page.' ) );
} else {
if ( ! current_user_can( 'edit_post', $post_ID ) )
wp_die( __( 'You are not allowed to edit this post.' ) );
}
set_current_screen( $_POST['screen'] );
if ( $last = wp_check_post_lock( $post_ID ) ) {
$last_user = get_userdata( $last );
$last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
printf( $_POST['post_type'] == 'page' ? __( 'Saving is disabled: %s is currently editing this page.' ) : __( 'Saving is disabled: %s is currently editing this post.' ), esc_html( $last_user_name ) );
wp_die();
}
$data = &$_POST;
$post = get_post( $post_ID, ARRAY_A );
$post = add_magic_quotes($post); //since it is from db
$data['content'] = $post['post_content'];
$data['excerpt'] = $post['post_excerpt'];
// rename
$data['user_ID'] = $GLOBALS['user_ID'];
if ( isset($data['post_parent']) )
$data['parent_id'] = $data['post_parent'];
// status
if ( isset($data['keep_private']) && 'private' == $data['keep_private'] )
$data['post_status'] = 'private';
else
$data['post_status'] = $data['_status'];
if ( empty($data['comment_status']) )
$data['comment_status'] = 'closed';
if ( empty($data['ping_status']) )
$data['ping_status'] = 'closed';
// update the post
edit_post();
$wp_list_table = _get_list_table('WP_Posts_List_Table');
$mode = $_POST['post_view'];
$wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ) );
// INTERCEPT: Check if it is our post_type, if not, do nothing
if( 'post' == $_POST['post_type'] )
{
?>
<script type="text/javascript">so_13418722_doMove();</script>
<?php
}
// end INTERCEPT
wp_die();
}
I had the same need.
Nicola's solution worked for me except I was getting an "Undefined variable" notice/error. Then I realized that the parameter has to be without the "[$this ... ]" part.
I guess that was a copy/paste from the documentation.
So this worked:
add_filter( 'list_table_primary_column', 'list_table_primary_column', 10, 2 );
function list_table_primary_column( $default, $screen ) {
if ( 'edit-your_post_type' === $screen ) {
// Set default columns to Minutes Spent.
$default = 'your_column';
}
return $default;
}
If you don't know what your_column is, just inspect the title of that column and get the ID.
You can't move the actions per se without resorting to JS, as the accepted answer does. However, you can very easily build your own version of the actions popup with php and inbuilt Wordpress functions. This version will work even if the user has JS turned off.
Presuming you use a switch to populate your custom columns, do something likes this, if not adapt to your own function:
switch ( $column ) {
case 'your_column_name':
echo "Your custom content here";
my_custom_column_actions($post_id);
break;
}
Then have a separate function that recreates the actions popup.
function my_custom_column_actions($post_id) {
if($_GET['post_status']!='trash') :
$bare_url = "/wp-admin/post.php?post=$post_id&action=trash";
$nonce_url = wp_nonce_url( $bare_url, 'trash-post_'.$post_id );
echo " <div class='row-actions'>
<span class='edit'>
<a href='/wp-admin/post.php?post=$post_id&action=edit'>Edit</a> |
</span>
<span class='trash'>
<a href='$nonce_url' class='submitdelete'>Trash</a>
</span>
<span class='edit'>
<a href='".get_the_permalink($post_id)."'>View</a> |
</span>
</div>";
else:
$bare_url = "/wp-admin/post.php?post=$post_id&action=untrash";
$nonce_url = wp_nonce_url( $bare_url, 'untrash-post_'.$post_id );
$delete_url = "/wp-admin/post.php?post=$post_id&action=delete";
$nonce_delete_url = wp_nonce_url( $delete_url, 'delete-post_'.$post_id );
echo " <div class='row-actions'>
<span class='untrash'>
<a href='$nonce_url' class='untrash'>Restore</a> |
</span>
<span class='delete'>
<a href='$nonce_delete_url' class='submitdelete'>Delete Permanently</a>
</span>
</div>";
endif;
}
All the business with nonce_urls is important for trashing, restoring or deleting; these actions won't work without it.
If you want to include this on a column that would normally appear, e.g. author or date published, you'll need to not include the standard column when you're declaring your custom columns and instead include a custom column that gets the same data (plus a call to the function above).
The same goes for if you want to include the title but not have the actions appear under it - otherwise they'll appear twice. Don't include the title column and instead include your own custom column that grabs the title but not the new function.
You can also very easily add your own actions by just editing the content that the function echoes.

Conditional Tags for Custom Taxonomy in Custom Post Type

I do hope you give me a practical answer for this (maybe simple problem for you). I have crated a Custom Post Type named (example:) "Cuspost" and I have Custom Taxonomy inside Cuspost named "Custax". Then I have taxonomies: "A Custax" and "B Custax" inside Custax.
What I want to do is just want to check the value of the Custax, for example with has_custax('a-custax') (similar to has_category('a-category'));
Next using is for this:
<?php if (has_custax('a-custax')) {
echo 'do something A';
} else {
echo 'do something B';
}
For your reference, I've read this (http://wordpress.org/support/topic/custom-taxonomies-conditional-tags#post-1110167) and it aint work.
Thanks for help.
Solve with this function on functions.php similar to Justin Tadlock solutions
<?php function has_custax( $custax, $_post = null ) {
if ( empty( $custax ) )
return false;
if ( $_post )
$_post = get_post( $_post );
else
$_post =& $GLOBALS['post'];
if ( !$_post )
return false;
$r = is_object_in_term( $_post->ID, 'custax', $custax );
if ( is_wp_error( $r ) )
return false;
return $r;
}
?>
And this is the conditional tag. Can be used in/outside the loop:
<?php if ( has_custax( 'a-custax', $post->ID ) ) {
echo 'do something A';
} else { echo 'do something B'; }; ?>
Credit to my friend Sulton Hasanudin

Resources