1

I'm encountering a problem with a WordPress plugin I'm developing for DirectoryPress listing pages. The plugin aims to replace the default permalink with a new link specified by users in a custom field.

Despite multiple attempts, the permalink is not updating as expected, and I'm not receiving any error messages from WordPress.

Here's is the code:

/*
Plugin Name: Change Product Link
Description: Replace permalink with a new link on DirectoryPress listing pages.
Version: 1.0
Author: Your Name
*/

// Add custom field to listing edit page
function add_custom_field_to_listing_edit_page() {
    global $post;

    if ($post && $post->post_type == 'dp_listing') {
        $new_link = get_post_meta($post->ID, 'new_link', true);
        ?>
        <style>
            .form-field {
                margin-bottom: 15px;
            }
            .form-field label {
                display: inline-block;
                width: 150px;
                font-weight: bold;
            }
            .form-field input {
                width: 300px;
                max-width: 100%;
            }
        </style>
        <div class="form-field">
            <label for="new_link"><?php _e('New Link', 'change-product-link'); ?></label>
            <input type="text" id="new_link" name="new_link" value="<?php echo esc_attr($new_link); ?>" />
        </div>
        <?php
    } else {
        $error_message = __('Custom field could not be displayed. Make sure you are on a DirectoryPress listing edit page.', 'change-product-link');
        add_settings_error('custom_field_display_error', '', $error_message, 'error');
    }
}

add_action('edit_form_after_title', 'add_custom_field_to_listing_edit_page');

// Save custom field when listing is saved
function save_custom_field($post_id) {
    if (isset($_POST['new_link'])) {
        if (update_post_meta($post_id, 'new_link', sanitize_text_field($_POST['new_link']))) {
            return;
        } else {
            $error_message = __('Failed to save the new link.', 'change-product-link');
            add_settings_error('custom_field_save_error', '', $error_message, 'error');
        }
    }
}

add_action('save_post_dp_listing', 'save_custom_field');

// Replace permalink with new link on listing page
function replace_product_link($content) {
    if (is_singular('dp_listing')) {
        $new_link = get_post_meta(get_the_ID(), 'new_link', true);
        
        if ($new_link) {
            return '<a href="' . esc_url($new_link) . '">' . get_the_title() . '</a>';
        } else {
            $error_message = __('Failed to retrieve the new link.', 'change-product-link');
            add_settings_error('new_link_retrieval_error', '', $error_message, 'error');
        }
    }
    return $content;
}

add_filter('the_content', 'replace_product_link');

// Display any settings errors
function display_settings_errors() {
    settings_errors();
}

add_action('admin_notices', 'display_settings_errors');

If anyone has experience with WordPress plugin development or expertise in DirectoryPress listings, your assistance would be greatly appreciated!

Thank you for your help!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.