Get custom field value but hide first 19 and last 2 characters - wordpress

Geeks and code Gods,
I am having an issue with my wordpress code. I have added 3 extra columns to the dashboard's pages screen.
One of which displays the custom field 'scode'. The scode custom field holds the cart66 shortcode which adds an 'add cart' button to the page it is assigned to.
The shortcode looks like this:
[add_to_cart item="HD-NL-7010"]
Each page has a different shortcode.
Every shortcode is the same length, 31 characters long.
However Id prefer not to show the whole shortcode in the column, only the product code *HD-NL-7010* section of the code.
Is it possible to hide the first 19 and last 2 characters. Basically removing [add_to_cart item=" and "]
Below is the code I added to function.php to add the custom columns to the pages table:
function test_modify_post_table( $column ) {
$column['scode'] = 'Product Code';
$column['Price'] = 'Price';
$column['Product_Image'] = 'Image';
return $column;
}
add_filter( 'manage_pages_columns', 'test_modify_post_table' );
function test_modify_post_table_row( $column_name, $post_id ) {
$custom_fields = get_post_custom( $post_id );
switch ($column_name) {
case 'scode' :
echo $custom_fields['scode'][0];
break;
case 'Price' :
echo '£' . $custom_fields['Price'][0];
break;
case 'Product_Image' :
echo $custom_fields['Product_Image'][0];
break;
default:
}
}
add_filter( 'manage_pages_custom_column', 'test_modify_post_table_row', 10, 2 );
Thank you for reading this post. Any help would be greatly appreciated.
P.S. I'm going to ask this in another post but does anyone know how to get the Product_Image to show as an image in the page column rather than the image attachment ID?

I might be misunderstanding, does PHP's substr help? Something like:
case 'scode' :
echo substr( $custom_fields['scode'][0], 19, 10 );
break;
For what it's worth, I think I'd store just the code (HD-NL-7010) in the custom field, rather than the shortcode code, then apply the shortcode where needed (with do_shortcode).

Related

Print "custom label" for indicating type of post in the front end for Wordpress

I have a blog with different custom post types ("books", "interviews", "recipes", "events", etc...). those are all appearing in the home page with same format like a grid. I would like to print in the front end a "label" customized possibly, representing the kind/content of post.
For example:
if the post is a CPT "Book", I want to show in the grid cell "looking for a book?"
if the post is a CPT "Recipe", I want to show on same position, for specific post "hungry?"
etc...
Can you maybe help me in this? I guess I need some PHP code and set it with Elementor, but I am not a developer... :(
Thanks for any help.
Mario.
I have been asked in comment to put a screenshot. this is a fake grid taken from internet (I know, ugly layout), presenting in descending order by date all posts, very different in domain (different custom post types), which I am able to do it. What I need is, depending by Post Type, to add a slogan like "watch the movie" or "hungry?" or "Interview with...", a static string totally dependent by the type of CPT.
fake sample from internet
Further integration to explain the context.
See the current home page of my site: click here
You see two "posts" in a grid (3 columns, published with "post" widget in elementor and a custom skin. This custom skin is linked to a "loop" template created with "ele custom skin". As you see by the pic, you have one post which is a recipe (custom post type "Recipe") and one is a book (custom post type is book). But here I can eventually find also a standard post. Now, when you see the "red dot", I would like to put a word, which is directly dependent by the post type:
if "recipe" --> "Hungry?"
if "book" --> "our book reviews"...
etc...
as a sample I have in this link click here for each loop in the grid, called using "shortcode" widget
[helloworld]
and coded in snippets plugin following portion of php code
function HelloWorldShortcode() {
return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode');
Here is the code, from this code shortcode will be [vkh_display_post_tagline]
/**
* Custom shortcode to display the tagline by the post type.
*/
/**
* Custom shortcode to display the tagline by the post type.
*/
function vkh_display_tagline_by_post_type() {
global $post;
if ( ! $post || empty( $post ) ) {
return;
}
// Uncomment the next line for debugging and to know if we're getting post type of not.
// return $post->post_type;
$heading = '';
switch ( $post->post_type ) {
case 'recipe':
$heading = __( 'Hungry?', 'your-text-domain' );
break;
case 'book':
$heading = __( 'Our book reviews', 'your-text-domain' );
break;
// Add more cases following above examples.
default:
// If you have to assign anything default when a case doesn't match
// then write that here.
$heading = '';
break;
}
// When heading is not empty return heading within the html tag.
if ( ! empty( $heading ) ) {
return '<h4 class="post-tagline">' . esc_html( $heading ) . '</h4>';
}
}
add_shortcode( 'vkh_display_post_tagline', 'vkh_display_tagline_by_post_type' );

Display Elementor page to customer in Woocommerce My-Account

I have Woocommerce (Subscriptions) and Elementor. I'm trying to add a page/content within the Myaccount area of Woocommerce - new navigation menu item.
Creating the endpoint and navigation menu bit works without issue. The issue I'm facing is displaying a page created in elementor. One page (also created in elementor) works without issue while the other doesn't.
The page created in elementor is fairly simple that essentially creates 4 columns, 10 rows. Within each row there is button that uses shortcodes to get the button text and url to navigate to when pressed. This is all tested and works without issue when accessing the page directly.
If I use this code
$post = get_post(1114);
$content = apply_filters('the_content', $post->post_content);
echo $content;
on the endpoint to display the page the output is just a list of rows of text showing the table cells from left to right. This only shows the button text (no sign of the URL) and is not formatted in anyway like the page in the elementor editor is (or if accessed directly)
e.g if the table is
H1 H2 H3 H4
R1a R1b R1c R1d
R2a R2b R2d R2d
The display is
H1
H2
H3
R1a
R1b
R1c
R1d
R2a
R2b
R2c
R2d
If I use the below code
$content = \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( 1119);
echo $content;
the table largely displays correctly with all formatting etc. The one thing that isn't working is the button text. Instead of displaying the text returned by shortcode it just displays the shortcode.
I'm sure I'm just missing something that needs to be processed somewhere but I have no idea what it is and the Elementor pages don't give much away unfortunately.
Any help would be appreciated.
EDIT : The correct answer is that there is a dynamic button/dropdown next to the text field that I didn't notice before. Using this you can select shortcode and enter the shortcode details and it will display correctly without having to process manually.
Not sure why it doesn't load correctly as above in the first place but to get around it I did the below. No sure this is the correct way to do this but it works.
$content = \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( 1013 );
$content = process_subswitch_content_for_shortcodes( $content );
echo $content;
The below essentially searches the content returned from the get_builder_content_for_display function for '[subswitch' which is the start of the shortcodes in question (unable to just search for ] as elementor puts other [] in the content).
function process_subswitch_content_for_shortcodes( $content ) {
$keepprocessing = true;
$searchstart_pos = 0;
do {
$startchar_pos = strpos( $content, "[subswitch", $searchstart_pos );
if ( $startchar_pos == false ) {
$keepprocessing = false;
}
$endchar_pos = strpos( $content, "]", $startchar_pos );
$shortcode_request = substr( $content, $startchar_pos, $endchar_pos );
if ( $shortcode_request == false ) {
$keepprocessing = false;
}
$shortcode_content = do_shortcode( $shortcode_request );
$content = str_replace( $shortcode_request, $shortcode_content, $content );
$searchstart_pos = $endchar_pos;
} while ( $keepprocessing );
return $content;
}
I'd of course still like to know the way to load this directly so that it displays without having to process the shortcodes manually so to speak.

Woocommerce - Group by first letter

I am trying to display the products in my Shop page like that:
A
Asd
Asddd
B
Beer
Bear
and so on. I managed to do this for the categories by overriding and using the woocommerce_output_product_categories action and for them it works, but I want to do this for the products as well(since Woocommerce gives you the option to show products or categories in the shop page). Thank you!
There can be so many possible solutions. But for me this can be done like this:
add_action( 'woocommerce_shop_loop', 'wc_shop_loop', 30 );
function wc_shop_loop() {
global $product, $last_title_first_letter_95845949545454;
$title = $product->get_title();
if ( $last_title_first_letter_95845949545454 !== $title[0] ) {
$last_title_first_letter_95845949545454 = $title[0];
woocommerce_product_loop_end(); // let's close the loop.
echo '<h3>'.$last_title_first_letter_95845949545454. '</h3>'; // add a letter heading.
woocommerce_product_loop_start(); // open a new loop start.
}
}
Tested to work on shop page and product category page.
You will need to work on its css.

Wordpress shortcode linked to another DB

First thing first, this is my first shortcode attempt.
It is worthed mention that I'm also using woocommerce on my website.
Let's start:
I know that to add shortcodes into wordpress, you need to write something similar to the code below into the functions.php file: (this is just an example)
function myshortcode() {
return "This is a shortcode example!";
}
add_shortcode( 'mycode', 'myshortcode' );
and if i add [mycode] into the wordpress page editor, the preview shows my text correctly.
But what if i need to use a variable (in my case woocommerce order number) in the shortcode?
Let's say i need to compare woocommerce_order_number with my_custom_uid (inserted into another database, outside wordpress).
I usually use a db request like the one below, and usually it works fine (as before, this is only an example):
select 'my_custom_uid' from 'my_custom_database' where 'woocommerce_order_number' = '1234'
The problem is that i don't know the woocommerce_order_number (it changes everytime!), because this shortcode needs to go inside an html email body i need to send out to customers after they placed the order.
How can i get the customer woocommerce order (variable that changes everytime) so that i will be able to use it into my shortcode to link it to my custom_uid?
If the question is not clear enough, please feel free to ask for clarification!
thanks a lot
I don't see a reason to use a shortcode. If you want to add something to an email, you should use one of the hooks in the email. For example:
function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$some_field = get_post_meta( $order->id, '_some_field', true ),
$another_field = get_post_meta( $order->id, '_another_field', true ),
if( $plain_text ){
echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
} else {
echo '<p>The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field . '</p>';
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_meta', 30, 3 );
Note that the $order object is the first parameter available to the kia_display_email_order_meta function. So you'd be able to get the ID via $order->id. This should add the data after the customer address details, but there are other hooks available if woocommerce_email_customer_details isn't appropriate.
I finally managed to solve this, and here's what i did if someone is interested:
<?PHP
add_action('fue_before_variable_replacements', 'register_variable_replacements', 11, 4);
add_action('fue_email_variables_list', 'email_variables_list');
/**
* This gets called to replace the variable in the email with an actual value
* #param $var - Modify this: array key is the variable name, value is the replacement
*/
function register_variable_replacements($var, $email_data, $queue_item, $email){
global $wpdb;
// Look up UID from order number
$orderNumber = $var->get_variables()['order_number'];
$sql = " //Your sql statement here...//";
$results = $wpdb->get_results($sql);
$UID = $results[0]->meta_value;
$variables = array(
'uid' => $UID
);
$var->register($variables);
}
function email_variables_list($email)
{
global $woocommerce;
?>
<li class="var hideable var_subscriptions">
<strong>{uid}</strong>
<img class="help_tip" title="<?php _e('Order UID', 'follow_up_emails'); ?>" src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" width="16" height="16"/>
</li>
<?php
}
Now on your follow up email plugin, you can use {uid} as a variable and this will be replaced with the correct value on every email.
I though the best way was to use short code, but then i discovered this filter and i think this is the best way to handle this. This code is also pretty flexible and you can add as many variables as you want.
Hope this can help someone.

execute do_shortcode in functions.php

I'm trying to run a do_shortcode in functions.php with no luck
I'm using Types plugin http://wp-types.com/ for creating custom post types and custom fields.
What I'm trying do is adding a custom column in admin for view all custom posts that displays a thumbnail from a custom field.
This is what I got so far, but it seems that the shortcode doesn't work inside functions.php
// add a column for custom post type (products)
add_filter('manage_product_posts_columns', 'add_thumbnail_column');
add_action('manage_product_posts_custom_column', 'add_thumbnail_content', 10, 2);
function add_thumbnail_column($defaults)
{
$newSlice = array('thumbnail' => 'Image preview');
$counter = 2;
$array_head = array_slice($defaults,0,$counter);
$array_tail = array_slice($defaults,$counter);
$defaults = array_merge($array_head, $newSlice);
$defaults = array_merge($defaults, $array_tail);
return $defaults;
}
function add_thumbnail_content($column_name, $post_ID)
{
// this one works when putting into post content
echo do_shortcode('[types field="square-picture" id="' . $post_ID . '" size="thumbnail"]' . '[/types]');
}
Can anyone help please?
In the Wordpress notes for the function it says
"If there are no shortcode tags defined, then the content will be
returned without any filtering. This might cause issues if a plugin is
disabled as its shortcode will still show up in the post or content."
Types may be conditionally declaring their short code only when you are on the frontend. What may be happening is that, in the admin, the short code is not defined and you are simply getting a false return. While on the frontend, the shortcode is defined and you get the results you intended.

Resources