Hey Fellows - Wordpress RSS Widget URL manipulation

cottonwolf

Regular Member
Joined
Jan 20, 2015
Messages
469
Reaction score
242
I am making a WP site and am an amazon associate. I know programmers love challenges, so I'll ask here. If you tell me the answer, you can sell it as a plugin/widget whatever.

This widget is the rss feed importer side widget on wordpress 4.2.x.

The rss feed returns the feed from a specified amazon feed with the links to the products. However, the feed I get from has it's own associate tag that I want to replace with mine in places.

This is the feed url where I got the problem : http://rssfeeds.s3.amazonaws.com/goldbox

Wordpress returns the feed with their associate tag. I want to change that. Sample returns:

E69s97I


Link example below:

http://www.amazon.com/gp/product/null/ref=xs_gb_rss_A1FRT0QGM9CTO/?ccmID=380205&tag=theirassociateid-20

tag=associate-20


The above code is what I would want to swap in php code with my associate id.
Use yourassid-20 in code sample, please.

Plus the below code returns a Title URL to amazon, http://www.amazon.com/gp/goldbox .
I'd like to know how I can append my associate id to that either with ?tag=associate-20 or simple tag=associate-20

The below code is from a wp install root/wp-includes/default-widgets.php from line 1137 on my copy.

Thanks, any pointers would be appreciated.

Code:
/**
 * Display RSS widget options form.
 *
 * The options for what fields are displayed for the RSS form are all booleans
 * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
 * 'show_date'.
 *
 * @since 2.5.0
 *
 * @param array|string $args Values for input fields.
 * @param array $inputs Override default display options.
 */
function wp_widget_rss_form( $args, $inputs = null ) {
    $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
    $inputs = wp_parse_args( $inputs, $default_inputs );

    $args['number'] = esc_attr( $args['number'] );
    $args['title'] = isset( $args['title'] ) ? esc_attr( $args['title'] ) : '';
    $args['url'] = isset( $args['url'] ) ? esc_url( $args['url'] ) : '';
    $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;

    if ( $args['items'] < 1 || 20 < $args['items'] ) {
        $args['items'] = 10;
    }

    $args['show_summary']   = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary'];
    $args['show_author']    = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author'];
    $args['show_date']      = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date'];

    if ( ! empty( $args['error'] ) ) {
        echo '<p class="widget-error"><strong>' . sprintf( __( 'RSS Error: %s' ), $args['error'] ) . '</strong></p>';
    }

    if ( $inputs['url'] ) :
?>
    <p><label for="rss-url-<?php echo $args['number']; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label>
    <input class="widefat" id="rss-url-<?php echo $args['number']; ?>" name="widget-rss[<?php echo $args['number']; ?>][url]" type="text" value="<?php echo $args['url']; ?>" /></p>
<?php endif; if ( $inputs['title'] ) : ?>
    <p><label for="rss-title-<?php echo $args['number']; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label>
    <input class="widefat" id="rss-title-<?php echo $args['number']; ?>" name="widget-rss[<?php echo $args['number']; ?>][title]" type="text" value="<?php echo $args['title']; ?>" /></p>
<?php endif; if ( $inputs['items'] ) : ?>
    <p><label for="rss-items-<?php echo $args['number']; ?>"><?php _e( 'How many items would you like to display?' ); ?></label>
    <select id="rss-items-<?php echo $args['number']; ?>" name="widget-rss[<?php echo $args['number']; ?>][items]">
<?php
        for ( $i = 1; $i <= 20; ++$i ) {
            echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>";
        }
?>
    </select></p>
<?php endif; if ( $inputs['show_summary'] ) : ?>
    <p><input id="rss-show-summary-<?php echo $args['number']; ?>" name="widget-rss[<?php echo $args['number']; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> />
    <label for="rss-show-summary-<?php echo $args['number']; ?>"><?php _e( 'Display item content?' ); ?></label></p>
<?php endif; if ( $inputs['show_author'] ) : ?>
    <p><input id="rss-show-author-<?php echo $args['number']; ?>" name="widget-rss[<?php echo $args['number']; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> />
    <label for="rss-show-author-<?php echo $args['number']; ?>"><?php _e( 'Display item author if available?' ); ?></label></p>
<?php endif; if ( $inputs['show_date'] ) : ?>
    <p><input id="rss-show-date-<?php echo $args['number']; ?>" name="widget-rss[<?php echo $args['number']; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/>
    <label for="rss-show-date-<?php echo $args['number']; ?>"><?php _e( 'Display item date?' ); ?></label></p>
<?php
    endif;
    foreach ( array_keys($default_inputs) as $input ) :
        if ( 'hidden' === $inputs[$input] ) :
            $id = str_replace( '_', '-', $input );
?>
    <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $args['number']; ?>" name="widget-rss[<?php echo $args['number']; ?>][<?php echo $input; ?>]" value="<?php echo $args[ $input ]; ?>" />
<?php
        endif;
    endforeach;
}

Code:
/**
 * Process RSS feed widget data and optionally retrieve feed items.
 *
 * The feed widget can not have more than 20 items or it will reset back to the
 * default, which is 10.
 *
 * The resulting array has the feed title, feed url, feed link (from channel),
 * feed items, error (if any), and whether to show summary, author, and date.
 * All respectively in the order of the array elements.
 *
 * @since 2.5.0
 *
 * @param array $widget_rss RSS widget feed data. Expects unescaped data.
 * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
 * @return array
 */
function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
    $items = (int) $widget_rss['items'];
    if ( $items < 1 || 20 < $items )
        $items = 10;
    $url           = esc_url_raw( strip_tags( $widget_rss['url'] ) );
    $title         = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
    $show_summary  = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
    $show_author   = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] :0;
    $show_date     = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;

    if ( $check_feed ) {
        $rss = fetch_feed($url);
        $error = false;
        $link = '';
        if ( is_wp_error($rss) ) {
            $error = $rss->get_error_message();
        } else {
            $link = esc_url(strip_tags($rss->get_permalink()));
            while ( stristr($link, 'http') != $link )
                $link = substr($link, 1);

            $rss->__destruct();
            unset($rss);
        }
    }

    return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
}

I'm a php peasant, all I know is from youtube vids, which is nothing ATM.
 
it's pretty hard to help you, without seeing the whole code. But I think this should do the trick:
Code:
$link = str_replace($link, 'theirassociateid-20', 'yourassociateid-20');

Like this:

Code:
/**
 * Process RSS feed widget data and optionally retrieve feed items.
 *
 * The feed widget can not have more than 20 items or it will reset back to the
 * default, which is 10.
 *
 * The resulting array has the feed title, feed url, feed link (from channel),
 * feed items, error (if any), and whether to show summary, author, and date.
 * All respectively in the order of the array elements.
 *
 * @since 2.5.0
 *
 * @param array $widget_rss RSS widget feed data. Expects unescaped data.
 * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
 * @return array
 */
function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
    $items = (int) $widget_rss['items'];
    if ( $items < 1 || 20 < $items )
        $items = 10;
    $url           = esc_url_raw( strip_tags( $widget_rss['url'] ) );
    $title         = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
    $show_summary  = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
    $show_author   = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] :0;
    $show_date     = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;


    if ( $check_feed ) {
        $rss = fetch_feed($url);
        $error = false;
        $link = '';
        if ( is_wp_error($rss) ) {
            $error = $rss->get_error_message();
        } else {
            $link = esc_url(strip_tags($rss->get_permalink()));
            while ( stristr($link, 'http') != $link )
                $link = substr($link, 1);
                $link = str_replace($link, 'theirassociateid-20', 'yourassociateid-20');


            $rss->__destruct();
            unset($rss);
        }
    }


    return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
}
 
Back
Top