0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/**
* Plugin Name: WP Turbo Amazon Affiliate
* Plugin URI: https://wpturbo.dev
* Description: Adds Amazon affiliate links with a custom search query to your posts.
* Version: 1.0.0
* Author: WP Turbo
* Author URI: https://wpturbo.dev
* Text Domain: wpturbo
*/
namespace WPTurbo;
use WP_Turbo;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Adds a shortcode to display an Amazon affiliate link for Amazon Spain with a search query.
*
* @param array $atts Shortcode attributes.
* @return string HTML link styled as a button with the affiliate ID.
*/
function wpturbo_amazon_affiliate_link( $atts ) {
$atts = shortcode_atts( [
'search' => '',
'text' => 'Compra en Amazon',
], $atts, 'amazon_affiliate' );
$affiliate_id = 'malloshopp-21';
$search_query = ! empty( $atts['search'] ) ? $atts['search'] : 'cloradores salinos';
$search_query_encoded = urlencode( $search_query );
$url = "https://www.amazon.es/s?k={$search_query_encoded}&tag={$affiliate_id}";
$button_style = "display: inline-block; padding: 10px 20px; color: #fff; background-color: #ff9900; text-align: center; text-decoration: none; border-radius: 5px;";
return "<a href="{$url}" target="_blank" rel="nofollow" style="{$button_style}">{$atts['text']}</a>";
}
add_shortcode( 'amazon_affiliate', 'WPTurbowpturbo_amazon_affiliate_link' );
/**
* Adds a custom metabox to the post edit screen.
*/
function wpturbo_add_custom_metabox() {
add_meta_box(
'wpturbo_amazon_affiliate_metabox',
'Amazon Affiliate Link',
'WPTurbowpturbo_render_custom_metabox',
'post',
'side',
'default'
);
}
add_action( 'add_meta_boxes', 'WPTurbowpturbo_add_custom_metabox' );
/**
* Renders the custom metabox.
*
* @param WP_Post $post The current post object.
*/
function wpturbo_render_custom_metabox( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'wpturbo_save_custom_metabox_data', 'wpturbo_custom_metabox_nonce' );
$search_value = get_post_meta( $post->ID, '_wpturbo_amazon_search', true );
?>
<label for="wpturbo_amazon_search">Consulta de Búsqueda de Amazon</label>
<input type="text" id="wpturbo_amazon_search" name="wpturbo_amazon_search" value="<?php echo esc_attr( $search_value ); ?>" style="width: 100%;" />
<?php
}
/**
* Saves the custom metabox data when the post is saved.
*
* @param int $post_id The ID of the post being saved.
*/
function wpturbo_save_custom_metabox_data( $post_id ) {
if ( ! isset( $_POST['wpturbo_custom_metabox_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['wpturbo_custom_metabox_nonce'], 'wpturbo_save_custom_metabox_data' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['wpturbo_amazon_search'] ) ) {
return;
}
$search_data = sanitize_text_field( $_POST['wpturbo_amazon_search'] );
update_post_meta( $post_id, '_wpturbo_amazon_search', $search_data );
}
add_action( 'save_post', 'WPTurbowpturbo_save_custom_metabox_data' );
/**
* Automatically add the Amazon affiliate shortcode to each post's content.
*
* @param string $content The post content.
* @return string The modified post content.
*/
function wpturbo_add_affiliate_link_to_content( $content ) {
global $post;
$search_query = get_post_meta( $post->ID, '_wpturbo_amazon_search', true );
$search_query = ! empty( $search_query ) ? $search_query : 'cloradores salinos';
if ( ! empty( $search_query ) ) {
$shortcode_after_paragraph = "[amazon_affiliate search="{$search_query}" text="Mejores {$search_query} - Comprar en Amazon"]";
$shortcode_end_of_post = "[amazon_affiliate search="{$search_query}" text="Mejores ofertas en {$search_query} - Comprar en Amazon"]";
$affiliate_link_after_paragraph = do_shortcode( $shortcode_after_paragraph );
$affiliate_link_end_of_post = do_shortcode( $shortcode_end_of_post );
// Insert the affiliate link after the first paragraph.
$content = preg_replace( '/</p>/', "</p>{$affiliate_link_after_paragraph}", $content, 1 );
// Append the affiliate link at the end of the content.
$content .= $affiliate_link_end_of_post;
}
return $content;
}
add_filter( 'the_content', 'WPTurbowpturbo_add_affiliate_link_to_content' );
/**
* Adds the Amazon search field to the quick edit screen.
*
* @param string $column_name The name of the column to edit.
* @param string $post_type The post type.
*/
function wpturbo_add_quick_edit( $column_name, $post_type ) {
if ( 'amazon_search' !== $column_name || 'post' !== $post_type ) {
return;
}
?>
<fieldset class="inline-edit-col-right">
<div class="inline-edit-col">
<label>
<span class="title">Amazon Search Query</span>
<span class="input-text-wrap"><input type="text" name="wpturbo_amazon_search" value=""></span>
</label>
</div>
</fieldset>
<?php
}
add_action( 'quick_edit_custom_box', 'WPTurbowpturbo_add_quick_edit', 10, 2 );
/**
* Enqueues the script for the quick edit functionality.
*/
function wpturbo_enqueue_quick_edit_script() {
wp_enqueue_script(
'wpturbo-quick-edit',
plugins_url( 'quick-edit.js', __FILE__ ),
[ 'jquery', 'inline-edit-post' ],
false,
true
);
}
add_action( 'admin_enqueue_scripts', 'WPTurbowpturbo_enqueue_quick_edit_script' );
/**
* Saves the custom field from the quick edit screen.
*
* @param int $post_id The ID of the post being saved.
*/
function wpturbo_save_quick_edit_data( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['wpturbo_amazon_search'] ) ) {
return;
}
$search_data = sanitize_text_field( $_POST['wpturbo_amazon_search'] );
update_post_meta( $post_id, '_wpturbo_amazon_search', $search_data );
}
add_action( 'save_post', 'WPTurbowpturbo_save_quick_edit_data' );
/**
* Adds the Amazon search column to the posts list table.
*
* @param array $columns The existing columns.
* @return array The modified columns.
*/
function wpturbo_add_amazon_search_column( $columns ) {
$columns['amazon_search'] = 'Amazon Search Query';
return $columns;
}
add_filter( 'manage_posts_columns', 'WPTurbowpturbo_add_amazon_search_column' );
/**
* Populates the Amazon search column with data.
*
* @param string $column_name The name of the column.
* @param int $post_id The ID of the post.
*/
function wpturbo_populate_amazon_search_column( $column_name, $post_id ) {
if ( 'amazon_search' === $column_name ) {
$search_query = get_post_meta( $post_id, '_wpturbo_amazon_search', true );
echo esc_html( $search_query );
}
}
add_action( 'manage_posts_custom_column', 'WPTurbowpturbo_populate_amazon_search_column', 10, 2 );
Y aquí tienes el contenido del archivo quick-edit.js:
Asegúrate de poner ambos archivos en la misma carpeta del plugin y luego sigue los pasos para subir e instalar el plugin desde tu panel de control de WordPress. Esto debería permitir que el plugin aparezca en la sección "Plugins" y puedas activarlo.