Display my block in a conditional-logic way - php

I use this plugin to filter my searches.
in this section they explain how to create my own custom block.
function prefix_register_block( $blocks ) {
// 'my_block' corresponds to the block slug.
$blocks['my_block'] = [
'name' => __( 'My Block', 'text-domain' ),
'render_callback' => 'prefix_my_block_render',
];
return $blocks;
}
add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );
// The render callback function allows to output content in cards.
function prefix_my_block_render() {
// Get current post, term, or user object.
$post = wpgb_get_post();
// Output the post title.
echo '<h3>' . esc_html( $post->post_title ) . '</h3>';
}
the code looks simple, so it's a matter of common sense and a php expert can figure it out quickly.
I would like to add a line to say
if slug or term "office" exist in the property-type taxnonomy
then Output the post title
(which I will of course edit to display custom fields)
but how to make conditional logic possible?
thank you for your patience, I do my best to understand the correct technique

I'm not a WP guru, but I think it should be this simple. Set up an array of slugs you want to affect in this way and then iterate through them in a foreach loop
function prefix_register_block( $blocks ) {
$slugs = ['office','somethingelse'];
foreach($slugs as $slug) {
$blocks[$slug] = [
'name' => __( ucwords($slug), 'text-domain' ),
'render_callback' => 'prefix_my_block_render',
];
}
return $blocks;
}
add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );
// The render callback function allows to output content in cards.
function prefix_my_block_render() {
// Get current post, term, or user object.
$post = wpgb_get_post();
// Output the post title.
echo '<h3>' . esc_html( $post->post_title ) . '</h3>';
}

Related

Add custom product fields to WooCommerce

In the past, I've added custom fields to my products in WooCommerce. This was only visible at the back and not directly visible on the front end.
Now I can't get it done in any way. When I work with plugins, this only works on the product page as a selection. But I would like to configure it on the product page in the back for example after the regular price field (see example).
I'm looking for the right hooks to do this. Anyone have an idea where I can find this?
add_action('woocommerce_product_options_pricing', 'add_custom_fields_to_poduct_general');
function add_custom_fields_to_poduct_general() {
global $woocommerce, $post;
woocommerce_wp_text_input(
array(
'id' => '_custom_text',
'value' => get_post_meta($post->ID, '_custom_text', true),
'label' => __('Custom text', 'woocommerce'),
)
);
}
Add more fields after price using the hook woocommerce_product_options_pricing
add_action('woocommerce_process_product_meta', 'save_custom_product_field');
function save_custom_product_field($post_id) {
$key = '_custom_text';
$value = (!empty($_POST[$key]) ? wc_clean(wp_unslash($_POST[$key])) : '' ); // phpcs:ignore WordPress.Security.NonceVerification
if ($value) {
update_post_meta($post_id, $key, $value);
} else {
delete_post_meta($post_id, $key);
}
}
The hook woocommerce_process_product_meta is used to save the data added using the above hook

Add metabox if there is at least one post available

So I am building a WordPress Dashboard widget which will showcase all posts & pages where a Gutenberg Block is active.
The below code does it's job and pulls in an array based on get_posts().
Here is what I'm attempting to do:
Is there a way that I can invoke the add_action and the pardot_dashboard_widget() function ONLY if there is at least one or more posts in get_posts()? If it's an empty array, don't even bother creating the metabox.
Here is the code:
/**
* Pardot Widget for Dashboard
*/
function pardot_dashboard_widget()
{
add_meta_box(
'pardot_dashboard_meta_box',
esc_html__( 'Pardot Form Locations', 'wporg' ),
'pardot_dashboard_stats',
'dashboard',
'side', 'high'
);
}
add_action('wp_dashboard_setup', 'pardot_dashboard_widget');
function pardot_dashboard_stats()
{
$args = [
's' => '<!-- wp:acf/pardot-form ',
'sentence' => 1,
'post_type' => [
'post',
'page'
],
];
$pardot_form_query = get_posts($args);
if (!$pardot_form_query) {
echo 'There are no active pardot forms available.';
}
}
You can try assigning the meta to a variable, and using that in the if statement.
For example:
<?php
$price_list = get_post_meta( $post->ID, 'price_list_category1', true );
?>
And then...
<?php
if ($price_list == '') {
echo ' ';
} else {
echo '<h2>' . add your meta box conditions here . '</h2>';
}
?>

Wordpress Shortcode will not return vars inside html string

I've tried everything I can think of. the title spits out at the top of the content wrapper and the html tags stay where they should.
I'm performing this shortcode from a page. I need the title and perma link to an image in the media library so that I can spit out html to show it on a page via shortcode
add_shortcode( 'ispimg', 'isp_gallery_item' );
function isp_gallery_item( $atts ) {
// Attributes
$a = shortcode_atts( array(
'id' => '',
), $atts );
$isp_post_id = get_post($a['id']);
setup_postdata($isp_post_id);
$pt = the_title();
return "<h3>".$pt."</h3>";
wp_reset_postdata();
}
Not totally sure if this is your issue, but I suggest you not to mess with the main query, you may be affecting other parts of your page because of that.
Use the API functions like get_the_title() instead:
function isp_gallery_item( $atts ) {
// Attributes
$a = shortcode_atts( array(
'id' => '',
), $atts );
return sprintf( '<h3>%s</h3>', get_the_title( $a['id'] ) );
}
add_shortcode( 'ispimg', 'isp_gallery_item' );
Or in case you need the loop, take a look at the WP_Query class.

WordPress - Update permalink automatically after posting an update

I am calculating custom title for a product using WooCommerce add product page. After the user post product's information, title is generated and saved by a save_post filter hook.
add_filter('save_post', 'modify_post_title', '99', 1);
function modify_post_title($post_id)
{
// some logic to form a new $title
// ...
if (!empty($title)) {
// update the title in database
$wpdb->update($wpdb->posts, array('post_title' => $title), array('ID' => $post_id));
// UPDATE PERMALINK
}
}
I need to know what function to use to re-generate the permalink after updating title.
Thanks in advance
add_filter( 'wp_insert_post_data', 'custom_slug_change', 50, 2 );
function custom_slug_change( $data, $postarr ) {
//Check for the post statuses you want to avoid
if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
return $data;
}
Would you please add above code in your functions.php ?
I think you can go with the window.history.pushState to maipulate the Browser History.
I think these may help you.
window.history.pushState("object or string", "Title", surl[0]);

How are action links for posts added in WordPress?

Certain WordPress plugins add links to the little mini-menu that shows up when hovering over post names in the "All Posts" section. This is what I'm talking about, like Purge from cache or Clone.
How do I add my own?
you use the add_filter hook
so for example if you want a link to search google for the page title.
add this to your functions.php
function search_google($actions, $page_object)
{
$actions['google_link'] = '' . __('Search Google for Page Title') . '';
return $actions;
}
add_filter('page_row_actions', 'search_google', 10, 2);
for a Custom Post Type
add_filter('page_row_actions', 'search_google', 10, 2);
function search_google($actions, $post)
{
if ($post->post_type =="YOUR_POST_TYPE"){
$actions['google_link'] = '' . __('Search Google for Page Title') . '';
return $actions;
}
}
more examples can be found here
The old version is not working anymore.
Now use this one:
add_filter('post_row_actions', 'search_google', 10, 2);
function search_google($actions, $post) {
$actions['google_link'] = '' . __('Search Google for Page Title') . '';
return $actions;
}
And for a custom post type:
add_filter('post_row_actions', 'search_google', 10, 2);
function search_google($actions, $post) {
if ($post->post_type =="YOUR_POST_TYPE"){
$actions['google_link'] = '' . __('Search Google for Page Title') . '';
return $actions;
}
}
In my situation, I wanted to both alter the behavior of the current links by having edit & view open in a new tab and add my own link specific to the Divi Builder theme. I wanted this to apply to pages, posts and all custom post types.
Because I am applying this change to both pages and posts, I had to do both the post_row_actions and page_row_actions add_filter actions.
This is the code added to my child theme functions.php file.
/* Modify row actions */
/* Modify row actions */
/* Modify row actions */
// Open Edit in new
function edit_new_tab( $actions, $page_object ) {
$actions['edit'] = '' . __( 'Edit' ) . '';
return $actions;
}
add_filter( 'post_row_actions', 'edit_new_tab', 10, 2 );
add_filter( 'page_row_actions', 'edit_new_tab', 10, 2 );
// Open View in new
function view_new_tab( $actions, $page_object ) {
$actions['view'] = '' . __( 'View' ) . '';
return $actions;
}
add_filter( 'post_row_actions', 'view_new_tab', 10, 2 );
add_filter( 'page_row_actions', 'view_new_tab', 10, 2 );
// Divi Builder in new
function add_divi_link( $actions, $page_object ) {
$actions['add_divi_link'] = '' . __( 'Divi Builder' ) . '';
return $actions;
}
add_filter( 'post_row_actions', 'add_divi_link', 10, 2 );
add_filter( 'page_row_actions', 'add_divi_link', 10, 2 );
In this code, view in $actions['view'] matches a built in row type. Because of this, the row type view is overwritten with the respective code. This also applies to edit. To view the row types and how they appear in your instance you can inspect the source code of the page and pay attention to the <span> that surrounds the link you want to modify. The class of the <span> will be the name you will use in $actions['span_class_goes_here']. Therefore, $actions['edit'] will determine that the Edit link will be modified.
In the case of $actions['add_divi_link'], add_divi_link does not exist in my scenario so it will be added as a new row action.

Categories