How do i Replace function from wordpress plugins? - wordpress

I am starting to study action and filter in wordpress. Lets say i wanna add DIV wrap to the output of printif at the bottom of code. Can i just copy all this code into my functions.php and edit some of its codes?
defined( 'YITH_WOOCOMPARE' ) || exit; // Exit if accessed directly.
if ( ! class_exists( 'YITH_Woocompare_Frontend' ) ) {
/**
* YITH Custom Login Frontend
*
* #since 1.0.0
*/
class YITH_Woocompare_Frontend {
public function __construct() {
// Add link or button in the products list.
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_product_page', 'yes' ) ) {
add_action( 'woocommerce_single_product_summary', array( $this, 'add_compare_link' ), 35 );
}
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_products_list', 'no' ) ) {
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_compare_link' ), 20 );
}
return $this;
}
public function add_compare_link( $product_id = false, $args = array() ) {
extract( $args ); // phpcs:ignore
if ( ! $product_id ) {
global $product;
$product_id = ! is_null( $product ) ? $product->get_id() : 0;
}
// Return if product doesn't exist.
if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) ) {
return;
}
$is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button', 'button' ) : $button_or_link;
if ( ! isset( $button_text ) || 'default' === $button_text ) {
$button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) );
do_action( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text );
$button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' );
}
printf( '%s', $this->add_product_url( $product_id ), 'compare' . ( 'button' === $is_button ? ' button' : '' ), $product_id, $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}

Related

Custom wysiwyg textarea - html text format

I created a custom field. Saving to db is OK. But when I save a page, the text is not saved as formatted but with html tags.
function sidebar_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function sidebar_add_meta_box() {
add_meta_box(
'sidebar-sidebar',
__( 'sidebar', 'sidebar' ),
'sidebar_html',
'page',
'advanced',
'default'
);
}
add_action( 'add_meta_boxes', 'sidebar_add_meta_box' );
function sidebar_html( $post) {
wp_nonce_field( '_sidebar_nonce', 'sidebar_nonce' ); ?>
<p>bočný stlpec ak by bolo treba</p>
<?php
$content = sidebar_get_meta( 'sidebar_sidebar' );
$editor_id = 'sidebar_sidebar';
$settings = array(
'media_buttons' => true,
'quicktags' => true,
'tinymce' => true,
'textarea_rows' => 5
);
wp_editor( $content, $editor_id, $settings );
}
function sidebar_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['sidebar_nonce'] ) || ! wp_verify_nonce( $_POST['sidebar_nonce'], '_sidebar_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( isset( $_POST['sidebar_sidebar'] ) )
update_post_meta( $post_id, 'sidebar_sidebar', esc_attr( $_POST['sidebar_sidebar'] ) );
}
add_action( 'save_post', 'sidebar_save' );
When I save the content, it displays it as follows:
<strong>Lorem ipsum</strong> dolor sit amet...
It should display it without html tags. Lorem ipsum must be bold.
You can use following code
function sidebar_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function sidebar_add_meta_box() {
add_meta_box(
'sidebar-sidebar',
__( 'sidebar', 'sidebar' ),
'sidebar_html',
'page',
'advanced',
'default'
);
}
add_action( 'add_meta_boxes', 'sidebar_add_meta_box' );
function sidebar_html( $post) {
wp_nonce_field( '_sidebar_nonce', 'sidebar_nonce' ); ?>
<p>bočný stlpec ak by bolo treba</p>
<?php
$content = sidebar_get_meta( 'sidebar_sidebar' );
$editor_id = 'sidebar_sidebar';
$settings = array(
'media_buttons' => true,
'quicktags' => true,
'tinymce' => true,
'textarea_rows' => 5
);
wp_editor( $content, $editor_id, $settings );
}
function sidebar_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['sidebar_nonce'] ) || ! wp_verify_nonce( $_POST['sidebar_nonce'], '_sidebar_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( isset( $_POST['sidebar_sidebar'] ) )
update_post_meta( $post_id, 'sidebar_sidebar', stripslashes( $_POST['sidebar_sidebar'] ) );
}
add_action( 'save_post', 'sidebar_save' );

woocommerce - Display Products in View orders page

I'm working on displaying the order products in the View Orders page in the customer account page like this
Can we do this?
Add this to your theme's 'functions.php'.
function add_products_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// add products after order total column
if ( 'order-total' === $key ) {
$new_columns['order-products'] = __( 'Products', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_products_my_account_orders_column' );
function add_products_data_my_account_orders_column( $order ) {
//loop through products of the order
foreach( $order->get_items() as $item_id => $item ) {
$product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$is_visible = $product && $product->is_visible();
$product_permalink = apply_filters( 'woocommerce_order_item_permalink', $is_visible ? $product->get_permalink( $item ) : '', $item, $order );
echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '<p>%s', $product_permalink, $item['name'] ) : $item['name'], $item, $is_visible );
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong></p>', $item );
}
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'add_products_data_my_account_orders_column' );

Change & Link of Add To Cart Button if product already in cart

I am trying to change the link & text of the Add to Cart button on the archive page if the products is already in cart.
I only allow quantity one per product to be added.
But the default way of Woocommerce is to direct to the single page with the notice.
Instead I would like the user to either
See the text "View Cart" and direct the user to the cart
or
See the text "Added" and have no link whatsoever.
Thank you
To change the text, you need the codes below.
add_filter('woocommerce_product_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
add_filter('woocommerce_product_single_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
function wc_product_add_to_cart_text( $text, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$text = "View Cart";
}
return $text;
}
To redirect to cart, this will do it.
add_action( 'wp_loaded', 'wc_add_to_cart_action', 19 );
function wc_add_to_cart_action(){
if ( empty( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
return;
}
$product_id = absint( $_REQUEST['add-to-cart'] );
$adding_to_cart = wc_get_product( $product_id );
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart && $adding_to_cart->is_sold_individually() ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
or use this for the second part.
add_action( 'woocommerce_simple_add_to_cart', 'wc_simple_add_to_cart' );
function wc_simple_add_to_cart(){
global $product;
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
echo sprintf( '<form class="cart">%s</form>', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ) );
}
}
update
for shop page you need this code:
add_filter('woocommerce_product_add_to_cart_url', 'wc_product_add_to_cart_url', 10, 2 );
function wc_product_add_to_cart_url( $url, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart && is_shop() ) {
$url = wc_get_cart_url();
}
return $url;
}

Edit Title Tag Wordpress tag.php

I'am trying to build an Input field for setting a custom HTML title-tag on tag.php in Wordpress. I archived it on page.php with custom fields. But it have no idea how to get it working on tag.php.
In My Function i have now:
add_theme_support( 'title-tag' );
add_filter( 'document_title_parts', 'orweb_custom_title');
function orweb_custom_title( $title ) {
if ( ! is_singular() ) return $title;
$custom_title = trim(get_post_meta( get_the_id(), 'title', true ));
if( ! empty( $custom_title ) ){
$custom_title = esc_html( $custom_title );
$title['title'] = $custom_title;
}
return $title;
}
function orweb_custom_title_old($title){
if ( ! is_singular() ) return;
$custom_title = trim(get_post_meta( get_the_id(), 'title', true));
if( ! empty( $custom_title ) ){
$custom_title = esc_html( $custom_title );
$title = $custom_title;
}
return $title;
}
add_filter('wp_title', 'orweb_custom_title_old', 10, 2);
Found here: https://orbitingweb.com/blog/custom-seo-friendly-title-tags-wordpress/.

How to add custom filed and save it in wordpress?

Following code I tried out But it is not save value of custom Fileds.
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'product', 'side', 'high' );
}
In above I have add code it is display when posttype is product
function cd_meta_box_cb( $product)
{
$values = get_post_custom( $product->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : ”;
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
In above code it will added the metabox with textbox and if value in metabox then it is display
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
}
I want to save data in a wp_postmeta table. Above code i have tried out. I am beginner in wordpress.Can any give me suggestion ?
Use This code for creating custom fields for post.
class Rational_Meta_Box {
private $screens = array(
'post',
);
private $fields = array(
array(
'id' => 'custom-field-1',
'label' => 'custom field 1',
'type' => 'text',
),
array(
'id' => 'custom-field-2',
'label' => 'custom field 2',
'type' => 'text',
),
);
/**
* Class construct method. Adds actions to their respective WordPress hooks.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ) );
}
/**
* Hooks into WordPress' add_meta_boxes function.
* Goes through screens (post types) and adds the meta box.
*/
public function add_meta_boxes() {
foreach ( $this->screens as $screen ) {
add_meta_box(
'my-custom-fields',
__( 'my custom fields', 'wordpress' ),
array( $this, 'add_meta_box_callback' ),
$screen,
'advanced',
'high'
);
}
}
/**
* Generates the HTML for the meta box
*
* #param object $post WordPress post object
*/
public function add_meta_box_callback( $post ) {
wp_nonce_field( 'my_custom_fields_data', 'my_custom_fields_nonce' );
echo 'its for custom fields for post typle';
$this->generate_fields( $post );
}
/**
* Generates the field's HTML for the meta box.
*/
public function generate_fields( $post ) {
$output = '';
foreach ( $this->fields as $field ) {
$label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
$db_value = get_post_meta( $post->ID, 'my_custom_fields_' . $field['id'], true );
switch ( $field['type'] ) {
default:
$input = sprintf(
'<input %s id="%s" name="%s" type="%s" value="%s">',
$field['type'] !== 'color' ? 'class="regular-text"' : '',
$field['id'],
$field['id'],
$field['type'],
$db_value
);
}
$output .= $this->row_format( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}
/**
* Generates the HTML for table rows.
*/
public function row_format( $label, $input ) {
return sprintf(
'<tr><th scope="row">%s</th><td>%s</td></tr>',
$label,
$input
);
}
/**
* Hooks into WordPress' save_post function
*/
public function save_post( $post_id ) {
if ( ! isset( $_POST['my_custom_fields_nonce'] ) )
return $post_id;
$nonce = $_POST['my_custom_fields_nonce'];
if ( !wp_verify_nonce( $nonce, 'my_custom_fields_data' ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->fields as $field ) {
if ( isset( $_POST[ $field['id'] ] ) ) {
switch ( $field['type'] ) {
case 'email':
$_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
break;
case 'text':
$_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
break;
}
update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], $_POST[ $field['id'] ] );
} else if ( $field['type'] === 'checkbox' ) {
update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], '0' );
}
}
}
}
new Rational_Meta_Box;
So it will create custom fields in your post type and also saved it too.
follow this for the references https://developer.wordpress.org/plugins/metadata/creating-custom-meta-boxes/
Just replace your add_action( 'save_post', 'cd_meta_box_save' ); function and put below code.
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
}
}
Full code like ( it's working fine and also save custom field in postmeta table)
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'side', 'high' );
}
function cd_meta_box_cb( $product)
{
$values = get_post_custom( $product->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : '';
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
}
}
Take a look at Pods.
It is a very powerful plugin for customization.
http://pods.io/

Resources