I'm using ultimate member for years, I created a custom tab in Edit Profile to update a value. In fact my users (students) can edit (add or remove) their pre-registered courses. The problem is that when they try to add or remove course, an error occurs, "An error has encountered". Surprisingly, they can remove the course even though it shows an error, but they can't add any course. It was working flawlessly till the latest update of Ultimate member. The code is as follows:
* */
/* create new tab */
add_filter('um_account_page_default_tabs_hook', 'CoursesTab', 100 );
function CoursesTab( $tabs ) {
$tabs[800]['CoursesTab']['icon'] = 'um-faicon-pencil'; // tab icon
$tabs[800]['CoursesTab']['title'] = 'Registered Courses'; // tab title
$tabs[800]['CoursesTab']['submit_title'] = 'Update'; // button text
$tabs[800]['CoursesTab']['custom'] = true;
return $tabs;
}
/* make our new tab hookable */
add_action('um_account_tab__CoursesTab', 'um_account_tab__CoursesTab');
function um_account_tab__CoursesTab( $info ) {
extract( $info );
$output = UM()->account->get_tab_output('CoursesTab');
if ( $output ) { echo $output; }
}
/* Finally we add some content in the tab */
add_filter('um_account_content_hook_CoursesTab', 'um_account_content_hook_CoursesTab');
function um_account_content_hook_CoursesTab( $output ){
ob_start();
$id = um_user('ID');
$output = '<div class="um-field">';
$names = array('course','course');
$fields = array();
foreach( $names as $name ){
$fields[ $name ] = UM()->builtin()->get_specific_field( $name );
}
$fields = apply_filters('um_account_secure_fields', $fields, $id);
foreach( $fields as $key => $data ){
$output .= UM()->fields()->edit_field( $key, $data );
}
$output .= '</div>';
$output .= ob_get_contents();
ob_end_clean();
return $output;
}
/* ensure that the custom fields are updated when the account is updated */
add_action('um_account_pre_update_profile', 'getUMFormData', 100);
function getUMFormData(){
$id = um_user('ID');
$names = array('course','course'); // ADD THE META-KEYS HERE
foreach( $names as $name )
update_user_meta( $id, $name, $_POST[$name] );
}
I searched a lot, however, found no clue how to solve the issue.
Thank you
I’ve added codes in this function:
add_filter('um_account_content_hook_CoursesTab', 'um_account_content_hook_CoursesTab');
function um_account_content_hook_CoursesTab( $output ){
ob_start();
$id = um_user('ID');
$output = '<div class="um-field">';
$names = array('course','course');
$fields = array();
foreach( $names as $name ){
$fields[ $name ] = UM()->builtin()->get_specific_field( $name );
}
$fields = apply_filters('um_account_secure_fields', $fields, $id);
foreach( $fields as $key => $data ){
$output .= UM()->fields()->edit_field( $key, $data );
}
$output .= '<input type="hidden" name="um_account_nonce_'. esc_attr( 'coursestab' ).'" value="'.esc_attr( wp_create_nonce( 'um_update_account_coursestab' ) ).'" />';
$output .= '</div>';
$output .= ob_get_contents();
ob_end_clean();
return $output;
}
I’ve added a hidden nonce field:
$output .= '<input type="hidden" name="um_account_nonce_'. esc_attr( 'coursestab' ).'" value="'.esc_attr( wp_create_nonce( 'um_update_account_coursestab' ) ).'" />';
Please try it and let me know if you’re still encountering issue.
Regards,
Related
I got a ACF Reaper field with a couple of rows I am trying to show. However, I only want the show row if it has a checkbox checked (Checkbox is a subfield within the repeater). I am trying to accomplish that by using if in_array as described in ACF documentation under "Conditional logic":
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) )
I am outputting the result in a WordPress shortcode. For now, my code kinda works, except it shows all results within the repeater field (also those that are unchecked). What am I missing ??
My code:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo get_field( 'upload_dokument' );
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
Managed to solve issue and with the help from #maggiathor's answer. For some reason echo was causing issue. I had to use return insted:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
$content = '<ul class="dokumenter">';
foreach( $rows as $row ) {
if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$pdf = $row['upload_dokument'];
$content = $content . '<li>' . $pdf . '</li>';
}
}
}
$content = $content . '</ul>';
return $content;
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
You cannot use get_sub_field() within the foreach loop, either you need to use a have_rows-while-loop or access it from the associative array:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo $row['upload_dokument'];
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
I'm trying to send price, name and id of each item in the order, plus an extra static value to a single js variable formatted in this way,
items = eventID::price::name|eventID::price::name|eventID::price::name
I was trying to do it this way, but I'm getting an error on the last line which I mean to add a pipe if there is more than one product.
$line_items = $order->get_items();
//loop over line items
$wgItemCount = $order->get_item_count();
$wgItems = array();
foreach ( $line_items as $item ) {
$wgItem = array();
$wgItem ['eventId'] = '777777';
$wgItem ['eventId'] .= '::';
$wgItem ['price'] = $order->get_line_total( $item, true, true );
$wgItem ['price'] .= '::';
$wgItem ['name'] = $item->get_name();
if ($wgItemCount > 1) { //add divider if more than one item
wgItem .= '|';
}
$wgItems[] = $wgItem;
}
That hasn't worked so far, so I thought maybe I should create a custom object that contains the variables instead:
wgProduct = "77777", $order->get_line_total( $item, true, true ), $item->get_name();
and then call the items in a list later like
echo print ("|", wgProduct)
I had also tried using JSON to encode the data but the third party told me that is not going to work for their needs.
Edit: Code is now doing everything I need it to, but it's only pulling one item from the list instead of a string of all of them.
Here's what it looks like now based on the help I got here:
add_action( 'woocommerce_thankyou','wg_tracking' );
function wg_tracking( $order_id ) {
$order = wc_get_order( $order_id );
$shipping_total = $order->get_shipping_total();
$order_total = $order->get_total();
$currency = $order->get_currency();
$coupons = $order->get_coupon_codes();
$items = $order->get_items();
$total_exc_shipping = $order_total - $shipping_total;
$order_discount = $order->discount_total;
foreach( $coupons as $coupon ){
$coupon_post_object = get_page_by_title($coupon, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_object->ID;
$coupon = new WC_Coupon($coupon_id);
}
$wgItems = array();
foreach ( $items as $item ) {
$line = '';
$line .= '77777';
$line.= '::' . $order->get_line_total( $item, true, true );
$line .= '::' . $item->get_name();
$line .= '::' . $item->get_id();
}
$wgItems[] = $line;
$itemsList = implode('|', $wgItems);
?>
<script>
(function(w,e,b,g,a,i,n,s){w['ITCVROBJ']=a;w[a]=w[a]||function(){
(w[a].q=w[a].q||[]).push(arguments)},w[a].l=1*new Date();i=e.createElement(b),
n=e.getElementsByTagName(b)[0];i.async=1;i.src=g;n.parentNode.insertBefore(i,n)
})(window,document,'script','https://analytics.webgains.io/cvr.min.js','ITCVRQ');
ITCVRQ('set', 'trk.programId', 88888);
ITCVRQ('set', 'cvr', {
value: '<?php echo $total_exc_shipping ?>',
currency: '<?php echo $currency ?>',
language: 'de_DE',
eventId: 77777,
orderReference : '<?php echo $order_id ?>',
comment: '',
multiple: '',
checksum: '',
items: '<?php echo $itemsList ?>',
voucherId: '<?php if ( $order_discount > 0 ) echo $coupon->get_code(); ?>'
});
ITCVRQ('conversion');
</script>
<?php
}
?>
I think this all can be simplified to just use array's implode() method.
Below are what I would do in your situation:
$wgItems = array();
foreach ( $line_items as $item ) {
$line = '';
$line .= '777777';
$line .= '::' . $order->get_line_total( $item, true, true );
$line .= '::' . $item->get_name();
$wgItems[] = $line;
}
$items = implode('|', $wgItems);
I made a function for Woo store to display custom taxonomies. And somehow my span conatiners for each are destroyed. Here's the code:
add_action( 'woocommerce_product_meta_start', 'add_my_meta', 1 );
function add_my_meta() {
$series = the_terms($post->ID, 'series');
if ($series) {
$meta_output = '<span style="display:block;">Series: ';
$meta_array = array();
foreach ($series as $serie) {
$meta_array[] = '' . $serie->name . '';
}
$meta_output .= join( ', ', $meta_array ) . '</span>';
}
return $meta_output;
}
So expected output is <span style="display:block;">Series: My Series</span>
Current output is My Series
Spans and text removed. Never faced that problem before, what's the problem and how to solve?
Found some mistakes — needed to use get_the_terms (was the_terms) and $serie->term_id (was $serie->slug)
add_action( 'woocommerce_product_meta_start', 'add_my_meta', 1 );
function add_my_meta() {
$series = get_the_terms($post->ID, 'series');
if ( is_array($series) ) {
$meta_array = array();
foreach ($series as $serie) {
$meta_array[] = '' . $serie->name . '';
}
echo '<span class="tagged_as">Series: ' . implode( ', ', $meta_array ) . '</span>';
}
}
I wrote this plugin that perfectly receives and shows data from an external API.
Code:
function get_posts_via_rest() {
$request = wp_remote_get( 'http://xxx' );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$risposta_json = json_decode( $body );
//print_r($data);
if( ! empty( $risposta_json ) ) {
foreach($risposta_json->data->offerte->data as $offerta ){ // now iterate through that array
if($offerta->struttura->id == aggancio_id_campo_uno()){ // check all conditions
echo '<ul>';
echo '<li>'.$offerta->struttura->nome .'</li>';
echo '<li>'.$offerta->struttura->url_completo .'</li>';
echo '<li>'.$offerta->titolo .'</li>';
echo '</ul>';
}
}
}
}
// Register as a shortcode to be used on the site.
add_shortcode( 'sc_get_posts_via_rest', 'get_posts_via_rest' );
Now I have created a type of custom post called bids, as this code shows all the bids for a given company id, which is set with an input field that I didn't put here because it was superfluous, my question is how do I now have the data from json to register them inside the db?
May this help you:
global $wpdb;
$table = $wpdb->prefix.'you_table_name';
$data = array('column1' => 'data one', 'column2' => 123);
$format = array('%s','%d');
$wpdb->insert($table,$data,$format);
$my_id = $wpdb->insert_id;
I've got the following PHP code that I'm using to output a list of all custom taxonomy values, then group them into alphabetical order by first letter. This is working fine, except that the URL isn't being outputted. Anyone able to help?
<?php
$list = '';
$tags = get_terms( 'film-categories' );
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= '<div class="cat-group"><h3>' . apply_filters( 'the_title', $letter ) . '</h3>';
$list .= '<ul>';
foreach( $tags as $tag ) {
$url = esc_attr( get_tag_link( $tag->term_id ) );
$name = apply_filters( 'the_title', $tag->name );
$list .= '<li>' . $name . '</li>';
}
$list .= '</ul></div>';
}
}
} else $list .= '<p>Sorry, but no tags were found</p>';
echo $list;
?>
I'm afraid you've confused.
According to your second line - you're fetching terms of custom tax and not tags.
$tags = get_terms( 'film-categories' );
Therefore , any function related to tags won't work correctly.
In order to get the url of the term use the get_term_link() function.
Just replace the current line with:
$url = esc_attr( get_term_link( $tag ) );
Should work.