Deviant Media LLC
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: API not updating article featured image returning 200I see that you have debug logging for success but not error. Have you tried something such as
else: logger.error(f"Failed to update featured image. Status code: {response.status_code}, Response: {response.content}, Media ID: {media_id}, Endpoint: {endpoint}")
Also, are you attaching the media to the POST ID directly? If not, have you tried that?
Forum: Fixing WordPress
In reply to: Homepage Change Coding ProblemBy gray box are you talking about the dark grey top header bar? Or the background image behind the main header?
#main-header { background: url(/wp-content/uploads/2017/02/header.jpg) 100%; background-color: rgba(0, 0, 0, 0); }
Forum: Developing with WordPress
In reply to: Expand all images on clickAre you looking for something like a lightbox? If so, here is a simple plugin https://www.ads-software.com/plugins/simple-lightbox/
Forum: Everything else WordPress
In reply to: How to Restrict Editor Access on ONLY HomepageAdd the following to your theme functions.php file:
function restrict_editor_from_homepage() { if (!is_admin()) { return; } // Ensure the function get_current_screen is defined if (!function_exists('get_current_screen')) { return; } $screen = get_current_screen(); if ('page' === $screen->id && isset($_GET['post'])) { $post_id = $_GET['post']; $home_page_id = get_option('page_on_front'); if ($post_id == $home_page_id) { $user = wp_get_current_user(); if (in_array('editor', $user->roles)) { wp_die('You do not have permission to edit the homepage.'); } } } } add_action('current_screen', 'restrict_editor_from_homepage');
This checks to see if the page is frontpage/homepage and checks users role for editor. If it matches, it will show a message “You do not have permission to edit the homepage.”
Forum: Developing with WordPress
In reply to: How to add a custom link on admin dashboard ?@dscp8g Try this:
add_action('admin_menu', 'custom_menu'); function custom_menu() { add_menu_page( 'Page Title', // The text to be displayed in the title tags of the page when the menu is selected 'Custom Menu', // The text to be used for the menu 'manage_options', // The capability required for this menu to be displayed to the user 'custom-menu-slug', // The slug name to refer to this menu by (should be unique for this menu) 'custom_menu_page', // The function to be called to output the content for this page 'dashicons-welcome-write-blog', // Icon URL 6 // Position in the menu order ); } function custom_menu_page() { // JavaScript to open the link in a new tab echo '<script type="text/javascript"> window.open("https://www.deviant.media", "_blank"); </script>'; // Display a message on the page echo '<div class="wrap"><h1>External Link Opened</h1><p>The webpage has been opened in a new window/tab.</p></div>'; }
Forum: Developing with WordPress
In reply to: How to add a custom link on admin dashboard ?To add a custom menu to the admin bar do the following and adjust as necessary.
add_action('admin_bar_menu', 'custom_menu', 100); function custom_menu($wp_admin_bar) { $args = array( 'id' => 'custom_menu', 'title' => '<span class="ab-icon dashicons-welcome-write-blog"></span> Custom Menu', // title of the menu item with icon 'href' => 'https://www.deviant.media', // change URL here 'meta' => array( 'class' => 'custom-menu-class', // you can add more classes here for styling 'title' => 'Visit Deviant Media', // tooltip 'target' => '_blank' ) ); $wp_admin_bar->add_node($args); }
Forum: Fixing WordPress
In reply to: Rest Api failed to publish 15k character word post contentHere are a few areas to investigate:
- PHP Configuration Limits: Check your server’s PHP configuration, specifically the
post_max_size
andmemory_limit
settings. A post with a large amount of content could exceed these limits, causing the request to fail. You might need to increase these values in yourphp.ini
file. - Database Configuration: Large posts could also be hitting limits within your database configuration or the database server’s capacity to handle large inserts. Check your MySQL (or database server) settings, particularly the
max_allowed_packet
parameter, which might need to be increased. - WordPress Configuration: Although unlikely, it’s worth checking if there are any theme or plugin conflicts that might interfere with inserting large posts. Sometimes, plugins or themes can introduce unexpected behavior or limitations.
- Web Server Configuration: Your web server (e.g., Apache, Nginx) might have configuration limits on the size of the request body. For Apache, this is controlled by the
LimitRequestBody
directive, and for Nginx, theclient_max_body_size
directive. - Error Logging: Examine WordPress and server error logs for more specific messages regarding the failure. These logs can offer clues that pinpoint the exact issue.
- Timeout Issues: If the request takes too long to process, it could hit a timeout limit, resulting in an error. This can be addressed by increasing the timeout settings on your web server or PHP configuration.
Since the error message indicates a database insertion issue (
"code":"db_insert_error","message":"Could not insert post into the database."
), I’d particularly focus on database-related settings and logs to start. If you’ve recently updated WordPress or plugins, ensure compatibility and check for known issues related to post insertions.Forum: Fixing WordPress
In reply to: Posts have multiple URLThank for the additional information and hopefully I understood everything correctly. Since you are using Yoast SEO we will leverage its ability to assign a primary category for each post and have any additional categories and their URL’s redirect to it.
Here is the updated code:function redirect_to_yoast_primary_category() { if (is_single() && !is_admin() && class_exists('WPSEO_Primary_Term')) { global $post; // Instantiate WPSEO_Primary_Term with 'category' taxonomy $wpseo_primary_term = new WPSEO_Primary_Term('category', $post->ID); $primary_term_id = $wpseo_primary_term->get_primary_term(); if ($primary_term_id && !is_wp_error($primary_term_id)) { $primary_category = get_term($primary_term_id, 'category'); // Verify that get_term did not return an error and returned a valid WP_Term object if (!is_wp_error($primary_category) && $primary_category instanceof WP_Term) { $primary_category_slug = $primary_category->slug; $current_cat_slug = get_query_var('category_name'); // Proceed with redirection only if the current category slug is valid and not the primary category if (!empty($current_cat_slug) && $current_cat_slug !== $primary_category_slug) { $redirect_url = get_permalink($post->ID); // Ensure the redirect URL is a valid URL to avoid redirection to unexpected locations if (false !== filter_var($redirect_url, FILTER_VALIDATE_URL)) { wp_redirect($redirect_url, 301); // Permanent redirect with HTTP status code 301 exit; } } } } } } add_action('template_redirect', 'redirect_to_yoast_primary_category');
Forum: Fixing WordPress
In reply to: Drop down list in pageYou would use gravity for the dropdown and to build out the questions.
Forum: Fixing WordPress
In reply to: Posts have multiple URLYou would add the code to your functions.php file. Change the “news” slug to match your main category slug.
ChatGPT is accurate if you ask it correctly and at times it does require some redirection. I use it quite often in my field of work.Forum: Fixing WordPress
In reply to: Posts have multiple URLHere’s how you can adjust the function to specifically redirect to the “News” category URL when an article belongs to multiple categories but is accessed through a non-“News” category URL:
function redirect_to_news_category() { if (is_single() && !is_admin()) { global $post; // Define the main category slug $main_category_slug = 'news'; // Get current category slug from URL $current_cat = get_query_var('category_name'); // Check if the current category is not the main category and the post belongs to the main category if ($current_cat && $current_cat !== $main_category_slug) { // Check if the post belongs to the main category if (has_category($main_category_slug, $post->ID)) { // Build the redirect URL $redirect_url = site_url() . '/' . $main_category_slug . '/' . $post->post_name; wp_redirect($redirect_url, 301); // Permanent redirect to the main category URL exit; } } } } add_action('template_redirect', 'redirect_to_news_category');
What This Code Does:
- It checks if the current request is for a single post and not within the admin dashboard.
- It defines “news” as the main category slug and checks the current URL for a different category slug.
- If the accessed category is not “news” but the post belongs to the “news” category, it redirects to the URL that uses “news” as the category slug.
- This redirection is done using a 301 HTTP status code, indicating a permanent redirect, which is beneficial for SEO as it passes link equity to the redirected URL.
Deployment:
- Add this code to your theme’s
functions.php
file or a site-specific plugin. - Replace
'news'
with the actual slug of your main category if it’s different. - Test this on a staging site first to ensure it works as expected without impacting other functionalities or SEO negatively.
Important Considerations:
- Caching & Performance: If you’re using caching plugins or server-side caching, ensure they’re configured to handle redirects properly to avoid redirect loops or caching the wrong responses.
- SEO Monitoring: After implementing redirects, keep an eye on Google Search Console for any increase in 404 errors or other crawl issues.
- Backup: Always back up your website before making changes to the code.
This custom solution should address your specific requirement to redirect articles accessed through non-main category URLs to the main category URL, consolidating your SEO efforts and reducing duplicate content issues.
Forum: Fixing WordPress
In reply to: Drop down list in pageOne way I would do this is to use GravityForms and populate the users on the dropdown field. Then the questions, and/or other fields, can be conditional based upon the user selected.
Forum: Fixing WordPress
In reply to: This site can’t provide a secure connectionYour website loads for me on Firefox if I go to https://ladderlead.unaux.com/ but if you try loading it as a secured connection HTTPS, then it will not load. Do you have an SSL installed?
A workaround involves using the
admin_init
hook, combined with additional checks when rendering the post editor, to dynamically add page attribute support based on the post’s category. Below is a conceptual approach:- Use the
admin_init
hook to set up an action for when the post is being edited. - Check if the current post belongs to your specific category.
- If it does, add page attribute support to that post.
Here’s how you can implement it:
function wpse_custom_init() { // Hook into admin_init add_action( 'admin_init', 'wpse_conditional_page_attributes_support' ); } function wpse_conditional_page_attributes_support() { // Check if we're on the post edit screen $screen = get_current_screen(); if ( $screen->id !== 'post' ) return; // Exit if not editing a post // Check if the global $post is set, and get the post ID global $post; if ( !isset($post->ID) ) return; // Exit if no post is loaded // Check if the post belongs to the specific category $category_slug = 'your-category-slug'; // Replace with your category slug if ( has_category( $category_slug, $post->ID ) ) { // Add page attribute support for posts in this category add_post_type_support( 'post', 'page-attributes' ); } } // Hook our function to init add_action( 'init', 'wpse_custom_init' );
Replace
'your-category-slug'
with the slug of the category you’re targeting. This code snippet checks the current post’s category when you’re on the post edit screen and conditionally adds page attribute support for posts that belong to the specified category.Note: Since this approach uses the
admin_init
and checks the current screen, it’s specifically tailored for the WordPress admin area and is intended to work when editing posts. This method dynamically applies the post type support feature, and it may not persist outside of the context of post editing (e.g., it won’t affect front-end displays or API responses unless those contexts also perform similar checks).Forum: Developing with WordPress
In reply to: Custom Post Type Taxonomy ArchiveYour issue with the WordPress block theme not correctly displaying the custom category archive page instead of defaulting to the general archive template could stem from a few reasons. Here are some potential causes and solutions:1. Theme Hierarchy and Naming Convention
WordPress uses a specific template hierarchy to determine which template file to use for different types of content. For category archives, WordPress looks for template files in the following order:
category-slug.php
category-id.php
category.php
archive.php
index.php
Ensure your template naming follows WordPress conventions. Since you are working with a block theme and using the Site Editor to create templates, the issue might be less about naming and more about how WordPress is recognizing your template for specific categories.2. Flush Rewrite Rules
Sometimes, WordPress doesn’t recognize new templates or changes to the permalink structure until you flush the rewrite rules. This can be done simply by going to Settings > Permalinks and clicking Save Changes without making any changes. This process rewrites the
.htaccess
file and refreshes WordPress’s understanding of your site structure.3. Check for ConflictsPlugin or theme conflicts can cause issues with template loading. Try deactivating all plugins except for those required for your custom post types and taxonomy, and see if the correct template is loaded. If it works, reactivate your plugins one by one to identify the culprit.4. Custom Query in Template
If your category archive template is being selected but not displaying the correct posts, ensure you’re not overwriting the main query with a custom query without properly resetting it afterward. Use the
pre_get_posts
action hook to modify the main query if needed, rather than creating new queries in the template.5. Inspect the Template HierarchyDebugging tools like Query Monitor can help you understand which template files WordPress is trying to load and why it might be falling back to the
archive.php
or another template. This can give you insight into where the process is breaking down.6. Custom Code for Template SelectionIf none of the above solutions work, you might need to use a more manual approach by hooking into the
template_include
filter to force WordPress to use your custom category archive template under specific conditions. Here’s a basic example:add_filter( 'template_include', function( $template ) { if ( is_category( 'your-category-slug' ) && !is_singular() ) { $new_template = locate_template( array( 'your-custom-template.php' ) ); if ( '' != $new_template ) { return $new_template ; } } return $template; });
Replace
'your-category-slug'
with the slug of your category and'your-custom-template.php'
with the path to your custom template file within your theme.SummaryEnsure your custom template is correctly named and recognized by WordPress, flush rewrite rules, check for conflicts, and consider using debugging tools to inspect the template hierarchy. If all else fails, manually force template selection with custom code.
- PHP Configuration Limits: Check your server’s PHP configuration, specifically the