INSTRUCTION
stringlengths
11
999
RESPONSE
stringlengths
0
999
SOURCE
stringlengths
16
38
METADATA
dict
Display post from specific date How do I display posts by a specific date? If it is 15 July today, it'll display posts from about two or three days ago, like the 12th of July's posts. And tomorrow it'll display the 13th of July's post. How can I achieve this?
I think if you want to show recent posts posted 2-3 days ago, you can use date queries, provided by WordPress You can get more information about date queries from here: WordPress Date Queries For Example: If you want to show last three days posts, it can be done using the following snippet of code: //set the arguments $args = array( 'order' => 'DESC', 'date_query' => array( array( 'after' => '3 days ago', ), ), ); // get posts using the arguments $posts = get_posts($args); The above will return an array of posts from last 3 days to the current day. You can also use it like: 3 days ago, 1 week ago etc.
stackexchange-wordpress
{ "answer_score": 2, "question_score": 1, "tags": "posts, date" }
Taxonomy archive 404ing (not term archive) I may have just dug too deep into WordPress lately that I am not able to free my head & don't get this working as supposed to do. I want a simple taxonomy archive listing all taxonomy terms (not a term's archive), while it only 404s on my end. This is how it is set up: I have a CPT **'travel'** and a taxonomy called **'actions'** that classify different kind of travel options (e.g. by bike, sailing etc.). Term archives (with a taxonomy-actions.php template) work just fine. The URL is like www.domain.com/actions/by-bike. However, when I try to pull the taxonomy archive, which should then be located at www.domain.com/actions, I get a 404. Is there something basic I am missing or doing wrong? Isn't the taxonomy-actions.php supposed to be used for the taxonomy archive in the first place?
There is no concept of an archive of _terms_ in WordPress. Archives are collections of _posts_ , The Loop only outputs posts. If you want a page listing all terms in a taxonomy, create a page, assign it a custom page template, and output the taxonomy's terms via one of the API functions, like `get_terms`.
stackexchange-wordpress
{ "answer_score": 2, "question_score": 0, "tags": "custom post types, custom taxonomy, archives" }
Do we have to use ftp to recover from a bad functions.php edit? Just tried to make an edit to the functions.php on my site. As is often the case, wp didn't like it and threw a fit, locking me out of making any further changes. So now I have to use ftp to edit out the offending code. You could argue I should be more careful / expert at coding, but the question is: Isn't there an easier way to recover from messing up with a functions.php edit than having to ftp?
Here's a much better solution: Don't edit the theme's functions.php file. Don't put your custom code into that file either. If you have snippets of code for a site, put them into a custom plugin. Put each one into its own plugin, in fact. Separate them by functionality. I even made a handy dandy plugin to make this easier, called Pluginception. The nice thing about editing plugins in the WordPress plugin edit screen: If you screw it up and cause a fatal error, WordPress will detect it and disable the plugin. This might break your site, but it probably won't break the admin screens and you can fix your error and reactivate the plugin. Don't worry about the number of plugins. That's totally irrelevant. The code is what matters. The code has the same impact whether you put in a bunch of easily separated plugins or whether you shove it all together into one functions.php file that is dangerous to edit.
stackexchange-wordpress
{ "answer_score": 4, "question_score": 0, "tags": "functions, ftp" }
How to change the standard navigation menu color when using genesis I need to change the font color for a wordpress menu. I'm currently using genesis as my parent theme & putting all of the css changes for it in a child theme. I would like to change the font color of my primary navigation menu, where do I put these changes in wordpress? In the child theme style.css or at Appearance > Menus ?
Are you trying to change the background color? If so, use `background-color: #0066CC` instead of `color: #0066CC`
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "css, genesis theme framework" }
Why can't I display attachment_image_src with Custom Size? I'm trying to display an attachment with a custom size that I created with `add_image_size` called "custom-size-01" What the code returns is the word "Array" I've looked at the wordpress reference and been trying the `wp_get_attachment_image_src` reference and tried the example code it gives but i haven't been able to make it work. I haven't figured out what I'm doing wrong and could use any input to show me where I'm making a mistake. if($imageid){ echo "<img src=\""; echo wp_get_attachment_image_src( $imageid, 'custom-size-01', false); echo "\" width=\"100%\" >"; }
With wp_get_attachment_image_src you get a array with values corresponding to the (0) url, (1) width, (2) height, and (3) scale of an image attachment (from the wordpress doc). you code can be: if($imageid){ $image_attributes = wp_get_attachment_image_src( $imageid, 'custom-size-01', false); echo "<img src=\""; echo $image_attributes[0]; echo "\" width=\"100%\" >"; }
stackexchange-wordpress
{ "answer_score": 2, "question_score": 0, "tags": "shortcode, images" }
How many Wordpress SQL Queries per page? I am on a shared hosting which has a limitation at MySQL queries per hour. My shows unable to connect to database because of this. Can anyone tell me how many queries are per page on a wordpress site with default theme instaled on? PS: I haven't any plugin installed.
Wordpress uses a lot of plugins and is very query heavy but the in spite of that, the front page uses `59 queries` to generate itself. I think the default (on a vanilla WordPress install on the default theme) is `27 or something of that nature`. The larger the number of queries, the slower the page is going to load and the more load you are going to put on your MySql server. All of the above is true if you do not use inline or regenerative caching mechanisms. If you want to know more go through this link and you can also calculate the number of queries using this plugin Debug Bar plugin
stackexchange-wordpress
{ "answer_score": 1, "question_score": 4, "tags": "query, mysql, page specific settings" }
Register users by e-mail I want to use an email address instead of username to register and login my members. I've got the login part working using some of the code in this answer. Works fine but as expected wordpress still wants a username on the register page (custom, not the default). This line of code registers the user: `$user_signup = wp_insert_user( $user_data );`, and when registering I get the following error: > Cannot create a user with an empty login name. I'm guessing this can be solved by removing the appropriate filter, but I'm new to wordpress. What's the code to remove the username requirement?
This is a required field (< and I think that more than one function in the core of Wordpress relies on that. So I would not recommend to code around it or alter any other WP core files as the next update could render all these changes back to the default values.
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "filters, user registration" }
I need to delete all posts from my homepage & create a static front page I want my homepage to be clean and without any posts or comments from my visitors. I have tried to do almost everything, but it doesn't work. I don't want people to discus anything, all they have to do is view who we are and how to reach us. Can somebody tell me what to do.
Goto settings>Reading> and look for "Front Page Displays". Select the "A static Page" radio button and below that click the drop down for "Front Page" and select which page you want for your home page.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "comments, blog" }
Programmatically log in a wordpress user I am trying to programmatically log in a wordpress user, using the following function for that. public function auto_login( $user ) { $username = $user; if ( !is_user_logged_in() ) { $user = get_user_by( 'login', $username ); wp_set_current_user( $user->ID, $user->user_login ); wp_set_auth_cookie( $user->ID ); do_action( 'wp_login', $user->user_login ); } } Unfortunately it seems not to be working. That function is called from within a shortcode. Could this be the explanation ? Should I rather hook my function to some filter, before any content is output ? Also I would like to redirect the newly logged in user to a specific post based on a $_GET parameter, can I simply add a header redirect at the end of the function ? Thank you in advance for your help !
Do this on template_redirect hook. You can use wp_safe_redirect to redirect to different page in the blog. I can't give you the code because I don't from where you are getting the $user
stackexchange-wordpress
{ "answer_score": 1, "question_score": 3, "tags": "login, headers, http" }
Custom fields doesnt work well with any postTAB plugin Here is my problem: I tried to implement custom fields (text) within a post tab. I tried many post tab plugins and the imlementation to template was succesfull... Unfortunately, the content of the custom field is showing always on TOP of the content and not inside... I tried eveything and every plugin, still same issue. I tried so far: WP UI, Post UI, Easy responsive tabs, everything. My following code: <?php $tabContent = the_field('kotoba'); echo do_shortcode('[restabs alignment="osc-tabs-left" responsive="false"] [restab title="Tab number 1" active="active"]'.$tabContent.'[/restab] [restab title="Tab number 1" active="active"]'. the_field('text_field').'[/restab] [restab title="Tab number 4"]Tab 4 content goes here.[/restab][/restabs]'); ?>
Well, you use ACF for your custom field, right? In that case, replace the function "the_field" in your code with "get_field". the_field echoes the field already, so the field is echoed before your tab shortcodes return from the function do_shortcode and get echoed, which results in "having the content of the field before the tab" stuff. Happy Coding, Kuchenundkakao
stackexchange-wordpress
{ "answer_score": 1, "question_score": -1, "tags": "custom field, shortcode, tabs" }
do_shortcode close I am using the jscrollpane plugin on WordPress along with the advanced custom fields plugin. The jscrollpane plugin uses the following shortcodes: [vr_jsp] [/vr_jsp] I have two different custom fields that I need to put between their own set of the above shortcodes which are: <?php the_field('first_content_block'); ?> <?php the_field('second_content_block'); ?> I understand you can use the below code to do this but I am a bit stuck on what to use to output the custom field between the two shortcodes. <?php echo do_shortcode('[vr_jsp]CONTENT HERE[/vr_jsp]'); ?> Just to clarify I need the custom field (first_content_block) displayed where 'CONTENT HERE' is.
Use get_field instead of the_field. That way you can assign it to a varilable. $first_comment = get_field('first_content_block'); $second_comment = get_field('second_content_block'); echo do_shortcode("[vr_jsp] $first_comment, $second_comment [/vr_jsp]"); I haven't tried it, but see if that works.
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "custom field, loop, shortcode" }
No comments on static front page I have had this problem before. I want my homepage to be very simple, and I don't want it to be a discussion forum or a place where you can leave your comments. I wrote yesterday and got some good answers. However, I had to change the theme, because what I wanted to do, wasn't possible with the theme Alexandria. I have made a very simple front page, but when I want to erase the comment area from my page 2, it's just not possible. I have chosen a static page, I have unchecked all the markings in discussion. But nothing helps. Underneath the comments area there is a lot of writing about the HTML and what attributes and codes are legal. I would love to be able to remove it all. What can I do to remove it all?
This should not be do difficult to achieve. What I would suggest is to create yourself a child theme. It is quick and very easy. The reason for this is, **never** make changes to a theme/plugin that you are not the author of. Once you have your child theme up and running, copy the page.php template from the parent theme to your child theme and rename it front-page.php (as I suggested in your previous question, go and read about creating a static front page) Open front-page.php and delete everything comment related. This should be at the bottom of your template. Save this template. This should be enough
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "posts, comments" }
Get all categories of custom post even not assigned I know how to get all categories with `get_categories` and name custom taxonomy name, but wordpress only shows those categories those are assigned to a post. Those which are created, but not assigned, will not show up with any of these methods. So is there any function that shows all categories that created + assigned and not assigned. I am working in a admin panel to show all categories in a drop down list in a custom post type.
> I know how to get all categories with `get_categories` I think you've missed some of the arguments in `get_categories`. By default, empty categories (categories without posts) are hidden 'hide_empty' => 1, To get categories without posts, simply set `hide_empty` to `0` 'hide_empty' => 0, Here is an example $categories = get_categories( 'hide_empty=0' );
stackexchange-wordpress
{ "answer_score": 3, "question_score": 1, "tags": "plugins, functions" }
What's the best way to split admin-only functionality in the theme's functions.php file? I am customising a Wordpress admin area and so far so good with a number of add_filter and add_action calls in my theme's functions.php file. But I'm a bit worried because all this code will be executed on all pages, not just the admin area. I therefore went ahead with the below approach: if (is_admin()) require_once('admin-functions.php'); It works but is this the cleanest way of doing it?
The way you do it is fine. Also have a look at how Tom McFarlins WordPress Plugin Boilerplate does it: if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { require_once( plugin_dir_path( __FILE__ ) . 'admin/class-plugin-name-admin.php' ); add_action( 'plugins_loaded', array( 'Plugin_Name_Admin', 'get_instance' ) ); } Source
stackexchange-wordpress
{ "answer_score": 1, "question_score": 2, "tags": "filters, actions" }
WordPress 3.9 two menus in same position? I have a new WordPress theme and I want to add extra menu, but as WordPress 3.9 allows only one menu to add in one position/places in theme? How can I add two menus in one position? I purchased the theme and it has top navigation call 'top-navigation' I used that for my main menu. Below that I need another menu, but I can't add because only one menu can be add in one position in this theme. So I am struggling to add full width slim menu.
WordPress allows the addition of extra nav menus. Add this to your child themes functions file: register_nav_menus( array( 'secondary_menu' => 'My Second Menu' ) ); Add this to your header.php file or the file where you want to display your extra nav menu. <nav id="site-navigation" class="main-navigation" role="navigation"> <?php wp_nav_menu( array( 'theme_location' => 'secondary', 'menu_class' => 'nav-menu' ) ); ?> </nav><!-- #site-navigation --> Change the menu_class in the code to match your existing themes nav menu or you may need to add extra CSS to style it. **Resources:** < <
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "theme development, menus, navigation" }
How to call ajax in plugin file I am very new to ajax with wordpress . Here i am getting a selected option value in `var test` using bellow function when i alert it its showing a right value. This `var test` value i have to send using ajax and i want GET this value in PHP variable. So what i required to call ajax and get script variable value of `var test` in php in plugin file where i have both bellow function and select box. JQuery Funtion. jQuery("#time_slot").change(function() { var test= time_slot.value; alert(test); }); Select Box <select name="time_slot" id="time_slot" class="time_slot"> <option value="">Choose a Time</option> <option value="1:00 - 3:00">01:00 AM-03:00 AM</option> <option value="3:00 - 5:00">03:00 AM-05:00 AM</option> </select>
I have call ajax using bellow code its working good. Here i am sending the `variable time` to this page using post `$.post("'. plugins_url().'/woocommerce-booking/gettime.php",` var time=0; jQuery("#time_slot").change(function() { time=time_slot.value; $.post("'. plugins_url().'/woocommerce-booking/gettime.php", {name: time}, function(response){ console.log(response); } ); }); my gettime.php file will be look like bellow and getting response from my ajax call. <?php session_start(); echo $name= $_POST["name"]; file_put_contents("name.txt", $name) ?>
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "ajax" }
Save custom post type in different table I need to store custom post type post data into a custom table, not in wp_posts and wp_postmeta. Is it possible? anyone help please. Actually, I have created two custom post type named "truck" and "drivers". Then I have created a menu page in wordpress admin. Clicking this menu should list all drivers related to truck. There should be also drag and drop system of this interface so that I can assign any truck to any driver. After sorting, it should update table. So I need a new table for custom post type. !see my screenshot
While I am a big fan of tweaking wordpress as much as it could because it allows us to, I think the best way forward is to have the best algorithm before coding. Reading your question, I came across " `Actually, I have created two custom post type named 'truck' and 'drivers'` ". **Issue:** Linking two post types requires extra work as you are doing because natively it is not what Wordpress does. **Suggested Solution:** Do it, the way WP works naturally: `Link a Custom Post Type to a taxonomy`. So, instead of having 'truck' and 'drivers' as CPTs, have 'drivers' as CPT and 'trucks' as taxonomy! That saves you tons of work, and especially the risk of running into issues should WP Update. You can then create the custom functionality of dragging and dropping on CPT and taxonomy
stackexchange-wordpress
{ "answer_score": 5, "question_score": 3, "tags": "custom post types, database, array, table" }
Unable to load stylesheet via wp_enqueue_style I am using this <?php// Load the theme stylesheets function theme_styles() { // Load all of the styles that need to appear on all pages wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' ); } add_action('wp_enqueue_scripts', 'theme_styles'); ?> to add a stylesheet in my theme, but it's not loading the stylesheet
Yo have not opened the `php` tag correctly in the beginning of your snippet. try adding at least one space after `<?php` and right before the first `//`: <?php // Load the theme stylesheets function theme_styles() { // Load all of the styles that need to appear on all pages wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' ); } add_action('wp_enqueue_scripts', 'theme_styles'); ?>
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "wp enqueue style" }
Hide image gallery in Woocommerce if NO image I have a few products with no images but they template leaves a big empty space where the image should be, I was wondering if there was a way to hide this product gallery section only IF there are NO images uploaded to that product. Thanks Badger
WooCommerce adds classes to indicate when a product has a thumbnail specified, so you can address this with CSS. .product .images { display: none; } /* hide images div by default */ .product.has-post-thumbnail .images { display: block; } /* show images div when thumbnail is present */
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "images, woocommerce offtopic" }
Give access of horizontal scroling plugin to user role editor How do i give access to user role editor without using any plugin using bellow function function HSA_add_to_menu() { add_options_page('Horizontal scrolling announcement', __('Horizontal Scrolling', WP_hsa_UNIQUE_NAME), 'manage_options', 'horizontal-scrolling-announcement', 'HSA_admin_options' ); }
> > function HSA_add_to_menu() > > { > > add_options_page('Horizontal scrolling announcement', __('Horizontal Scrolling', WP_hsa_UNIQUE_NAME), 'edit_others_posts', 'horizontal-scrolling-announcement', 'HSA_admin_options' ); > > } > > > <
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "plugins, user access" }
Wordpress MU users - how are they organized? WP and WP MU are a bit different, for example in users. I'm not sure that I understand user functionality correctly. Tell me please what are the differences between WP and WP MU users? For example I know that MU-users register only once on the main site of a network (ie they shouldn't register on each subsite). Also I know that a user can eighter have or not have a subsite. And know that there is a possibility to add a user to a certain subsite. It seems that's all I know :) Is it possible for user to register oneself on a certain subsite (but not only in the network in general) to be already automatically assigned to a certain subsite right after registration? How? What is a possibility to add a user to a certain subsite for? Can I delete a user from a certain site? And so on...
The conceptual difference between users in a standalone site and a network site is that in a network the users do not belong to the site but to the network itself are are being automatically logged in into all the network site once they login to one. Most of the time the difference can be ignored but there are some edge cases in which the differences come to like, like trying to make an anonymous comment when you are logged in to the network. In a network you don't add users to a site but only to the network, then you give them permission to do some actions on the site they were registered for. In a standalone registration implies automatically some permissions. This implies that as a subsite admin in a network you can not modify a user as it is not the sites user but the network's one. The most that can usually be done is to change the users permissions for that subsite.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "multisite, users" }
Downgrade admin account by mistake I made a huge mistake changing my admin account role from 'administrator' to 'Basic User'. Don't know what to do. Any suggestions how should I proceed to retrieve my account role back again, please?
Usually, this should help: 1. Open your database (with phpmyadmin, navicat, etc) 2. Open the wp_user table and have a look for the ID of your admin user (usually 1) 3. Open the wp_usermeta table and search for the row that has the user_id of your user 4. paste the following string into the meta_value field of the row: `a:1:{s:13:"administrator";b:1;}` that should be it: login again and the user should be administrator again. One advice though: Some plugins put their admin rights into that field too - those are lost after that. But deactivating/activating of the plugins could solve that. Best way to avoid all that: Create a new user in the user table and give this user the new rights. With the new admin user you could give the old admin user the admin status back again. br from Salzburg, Johannes
stackexchange-wordpress
{ "answer_score": 5, "question_score": 0, "tags": "wp admin, user roles" }
Wordpress insert NOW() in TIMESTAMP column returns all zeros I want to insert a TIMESTAMP into my column's TIMESTAMP column, but I always get all zeros. Here is my insert: $now = 'NOW()'; // insert the date into the db $wpdb->insert( 'wp_date', array( 'name' => $name, 'date' => $now ), array( '%s', '%s' ) ); // end insert
I hadn't tried to reproduce it, but the likely reason is that what you _want_ to use in query is SQL function (`NOW()`) but what you _"tell"_ WordPress to use is a string. Declaring its format to be `%s` means it is sanitized and put in quotes, making it something like `'NOW()'` which MySQL probably sees as very invalid timestamp. You will have to either generate raw SQL query for this (without using insert helper) or generate and provide timestamp string PHP-side.
stackexchange-wordpress
{ "answer_score": 2, "question_score": 2, "tags": "wpdb" }
Twenty Fourteen responsive CSS I today install on wordpress new responsive theme ''Twenty Fourteen'', I try to edit css, try to change bg color of header menu, I can't found in css where i change bg color, I before do this a million times on old not responsive css, how i edit responsive css ? Thanks.
The background colour of the header in Twenty Fourteen is defined in /wp-content/themes/twentyfourteen/style.css at line #847: .site-header { background-color: #000; max-width: 1260px; position: relative; width: 100%; z-index: 4; }
stackexchange-wordpress
{ "answer_score": 1, "question_score": -1, "tags": "css, responsive" }
For better digital journalism, how to store a second title and excerpt? This is a digital journalism issue. As the technology evolves, digital journalism has found new best practises. One of them is to always write a predictive title, combined with a good excerpt. This predictive title, however, lacks that "x factor" that a non predictive title has. A great example is the news published by Time about Microsoft that will cut 18.000 jobs in the next year. The proper title of the news is "Microsoft to Cut Up to 18,000 Jobs", but on the home page the opening title was "Ctrl-Alt-Del". Simple, catchy. So, we want to be able to create two titles and two excerpts inside a post. What should be the most effective and less resource consuming solution? Two meta values that I use only on the front-page.php? I don't want to add to much weight to the post queries. Any pointers?
Custom fields are perfectly suited for this purpose. WP Ref. IF the metabox titled Custom Fields is not visible on your post edit screen, you may need to enable the view with the Screen Options menu at the top right of your editor. For each post, simply create the secondary title and assign it a value. Where you would like this information to display, (front-page.php template, based on your example) you are able to make use of pre-built WP functions such as the_meta() or get_post_meta() for output. The meta_key is the name for your secondary title and meta_value is the actual title itself. This method is useful for additional information you may also wish to input with each news post. If you prefer a plugin over custom coding, I would recommend Elliot Condon's Advanced Custom Fields. This plug-in brings powerful features to non-developers with very little overhead. WP plugin directory link
stackexchange-wordpress
{ "answer_score": 5, "question_score": 0, "tags": "title" }
changing Wordpress login page I'm trying to change the default Wordpress login/signup page by doing this: Adding this code anywhere in function.php file of my theme: function custom_login_css() { echo '&lt;link rel="stylesheet" type="text/css" href="'.get_stylesheet_directory_uri().'/login/login-styles.css" /&gt;'; } add_action('login_head', 'custom_login_css'); Creating a /login directory in my theme's folder and put login-styles.css in it and save it. .login form { margin-left:auto; margin-right:auto; padding:30px; border: 1px solid rgba(0,0,0,.2); background-clip: padding-box; background: rgba(255, 255, 255, 0.9); box-shadow: 0 0 13px 3px rgba(0,0,0,.5); overflow: hidden; } When I try to load < I get: `<link rel="stylesheet" type="text/css" href=" content/themes/blaszok/login/login-styles.css" />` above the page instead of applying to the page.
The `&lt;` at the start and the `&gt;` at the end cause the problem when echoing in your `custom_login_css` function. Replace them with < >. The `&lt;` and `&gt;` are HTML entities meaning that if you want to print to your page a HTML reserved character you have to use this entities so the browser will understand that you don't want to use the HTML characters as code but to display it. More about HTML entities : <
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "login" }
retrieve post data and insert it in seperate static html page without Wordpress I have a blog which runs on Wordpress and inside the Wordpress environment I'm fine and know how to display specific blogposts etc. But beside I want to insert specific Blog-posts in a static HTML Website which doesn't have a CMS and is standalone. Imagine the simplest case of <body> <p class="blogpost"> <!-- insert specific blogpost of my blog on static website --> </p> </body> is there any possible chance to retrieve a blogpost of Wordpress by PHP without running the site on Wordpress? Any clue how to start with?
You can try using the JSON API plugin. It has endpoints for latest posts etc that can be accessed via jQuery, I use it to power mobile apps that use WordPress for content management. You also have the ability to create your own endpoints if the defaults don't give you what you need. Good luck!
stackexchange-wordpress
{ "answer_score": 2, "question_score": 0, "tags": "php, posts, static website" }
Get a variable from object in array I'm trying to `echo` `get_the_category($id)[0]->slug` and this works on localhost but not on the server. The error is > Parse error: syntax error, unexpected '['.... Is there a workaround?
Probably your hosting server has a different and older version of php so this syntax is not supported. The above syntax is implemented in PHP 5.4. You have to do it like this: $category = get_the_category($id); echo $category[0]->slug; **Source:** <
stackexchange-wordpress
{ "answer_score": 4, "question_score": 2, "tags": "categories" }
Problem to show custom post type in archive page category wise I was facing problem to show custom post type post category wise. Then i found to solve the problem . i wrote below code in function page : function my_blog_category( $query ) { if ( is_post_type_archive( 'blog') && $query->is_main_query() ) { $action = (isset($_REQUEST['ac'])) ? $_REQUEST['ac'] : 'none'; if($action!='none') $query->set( 'category_name', $action ); } } add_action( 'pre_get_posts', 'my_blog_category' ); So when I click > local/wp/index.php/blog?ac=company-news it shows all company-new category's post. But main problem is that I upload all file to website server. Now filter is not working on my webserver. There I click > Mydomain/index.php/blog?ac=company-news it show all post, not filter by category. I don't why it not work on my live web server .
You need to check the information that their is in action variable. $action = (isset($_REQUEST['ac'])) ? $_REQUEST['ac'] : 'none'; var_dump("action: "+$action); With this line, you will be able to review it's content and you will see that you don't get the correct information. I guess that your dev database is out off synch with your local one. So, you need to create post/page/categories to review your code. Otherwise, I'm using a really nice plug-in to migrate your local to dev DB. Note: don't forget to push your uploads too =)
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "custom post types, categories, pre get posts" }
Display all posts/pages in a multiple selectbox So I'm looking for a way to show all posts/pages inside a multiple selectbox, which is used to select and store which page/posts has been checked in order to display some text on that page(custom plugin). I'm looking to get the id of the posts/pages so that I can save these and use on the frontend of the templates. I did use get_pages() but these dont output anykind of page id.
`get_pages` returns an array of post objects, so you would use `foreach` to iterate over each object and grab the ID and whatever else you want to use: <?php if ( $pages = get_pages( $args ) { echo "<select multiple>"; foreach ( $pages as $page ) { echo "<option value='{$page->ID}'>{$page->post_title}</option>"; } echo "</select>"; } ?>
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "posts, pages" }
Display WordPress Search I am trying to display the current WordPress search term on single.php, index.php, and category.php...basically when someone searches for something I'd like WordPress to remember what the last search term was and display it anywhere on my site, is that possible? The function `get_search_query` only works on search.php Thanks, Josh
Tom, You're right! I didn't know how templates function, this was not something major, I just wanted to get that value and then display that value on my single.php page - but now I see that's not how WordPress works. I decided to just leave off that functionality :-) Thanks, Josh
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "functions, search, customization" }
WordPress Single Conditional for Search Page Is there a way to filter single.php so that if someone came from search.php it would display one thing, but if they came from anywhere else it would display another? Thanks, Josh
I created an `if` statement to solve my problem: <? if (stripos(wp_get_referer(), "cat=")!==false) { echo "Came from Search"; } else { echo "Didn't come from Search"; } ?> I am doing a category search, so I check to see if cat= is in the referrer url (or search url), if it is I execute some code, if not I execute some different code :-) That's it! Thanks, Josh
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "search, conditional tags, single" }
wp_dropdown_users() to include null value Is there an argument for `wp_dropdown_users()` which would add a blank null value (no user selected)?
Use 'show_option_none' argument for this. wp_dropdown_users( array('show_option_none' => __('No user selected') ) );
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "users" }
What are the hierarchy of filenames while initialising a WordPress plugin? WordPress has recommended to have the filename similar to the name of the plugin. There is no other information about the hierarchy of filenames of a WordPress Plugin. Also I don't find plugins following any rule for naming the file which initialises the WordPress plugin. Most of the plugins use the filename as my-custom-plugin.php. Some plugins use the filename as my_custom_plugin.php, some as bootstrap.php and some as loader.php. And I even saw one plugin having filenames like my-custom-plugin.php and loader.php, where loader.php initialises the plugin not my-custom-plugin.php. How it works? How WordPress looks for the file which initialises the plugin?
Wordpress looks for the Plugin Header in the php files. The main Plugin file should have a header like this: <?php /** * Plugin Name: Name Of The Plugin * Plugin URI: * Description: A brief description of the Plugin. * Version: The Plugin's Version Number, e.g.: 1.0 * Author: Name Of The Plugin Author * Author URI: * License: A "Slug" license name e.g. GPL2 */ For more info, look here: < Happy Coding, Kuchenundkakao
stackexchange-wordpress
{ "answer_score": 1, "question_score": 2, "tags": "plugins, filesystem" }
setting up a wp cron job I am a bit new to wp-cron job. I think i understand the concept but lack the implementation. i read a lot of tutorials on how to implement including this post: Run function at specific time but still cant figure where (in which files) is best practice. The above link describes kind of well my issue. i want each day on a specific hour to run test.php file which is in my root folder of wordpress(where the wp-content and include and etc). i am well aware for the disadvantages of wp-cron (no traffic no cron) and i am fine with it. i dont want to workaround with unix cron. I want the test.php file to be called not earlier then a specific time each day. please help and specify the file in which the implementation should be made. thanks
I am using crony now and it works very well. < With this cron one can easily set php file to fire or custom code on your own intervals. this instead of other plugins which helps you with setting the custom intervals rather the code and schedule itself. (plug and play - no code changes is required)
stackexchange-wordpress
{ "answer_score": -2, "question_score": 0, "tags": "wp cron, cron" }
Need plugins and themes from clients site I need to grab a clients themes and plugins to migrate their website to our servers. I tried using All In One Migration but it didnt work and caused some errors. Now I am going to do a fresh install of Wordpress. I can get the database with the Migrate DB tool but have no idea how to get the themes or plugins. I obviously don't have access to FTP or hosting because the current hoster does not want to provide that information to make it easy for us to get the files. Any suggestions on plugins or methods to get the site files so I can migrate?
Without FTP access, if you have an administrative login to the WordPress admin you can grab the code from plugins and themes one file at at time. For themes go to Appearance > Editor For plugins go to Plugins > Editor Select the appropriate theme / plugin and go through the list of files downloading the code, css, etc. This will not have the images, but you should be able to reconstruct the plugins and themes from this and what is publicly available to download using a website scraping tool... albeit a tedious process. The best option would be to convince the current host to provide FTP access.
stackexchange-wordpress
{ "answer_score": 1, "question_score": -1, "tags": "plugins" }
Woocommerce query all products and categories I need to generate xml file from all my products. My current query shows all product names, but how to access categories for each products? All the products have categories and subcategories. My current query: $args = array( 'post_type' => 'product', 'orderby' => 'post_excerpt', 'order' => 'ASC', //'product_cat' => 'My Product Category', 'post_status' => 'publish' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) { $loop->the_post(); echo '' . get_the_title() . '<br /><br />'; }
You can use wp_get_post_terms() $categories = wp_get_post_terms(get_the_ID(), 'product_cat', array("fields" => "names")); print_r($categories); The names of the product categories are returned as an array so you can either loop through them with a foreach or you can turn it into a string for example: $categories_list = implode(",", $categories); If you need more than just the names, you can get return array of the term properties by changing the fields to 'all' $categories = wp_get_post_terms(get_the_ID(), 'product_cat', array("fields" => "all")); <
stackexchange-wordpress
{ "answer_score": 1, "question_score": 1, "tags": "wp query, woocommerce offtopic" }
get_adjacent_post_link excluded_terms ignored I call the function ´get_adjacent_post_link´ using the argument ´$excluded_terms´. But this argument is ignored. This is how I call the function. get_adjacent_post_link( '%link', '<span class="forward"></span>', false, array(83, 88, 89, 182), true ); By clicking the resulting link I can reach a post that is in the category 'Blog Eintrag (de)' which has this admin URL /wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=83&post_type=post I think that a post with this category should be excluded by the argument ´array(83, 88, 89, 182)´. Why is it not? My Wordpress version is 3.9.1.
This question has the same solution as that one. The answer is short, so I post it here as well: > In WordPress 3.9 (and still in 3.9.1) there's a bug concerning the excluded terms in get_adjacent_post(). There's a plugin to fix that bug.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "categories, taxonomy, navigation, terms" }
When moving a WP site, why does wp-admin redirect to old site? I am in the process of moving a WP site from one host to another. I copied the database and all the files, created a new database and imported the old one. I then changed the wp-config file. The home page shows fine, but when I try to access /wp-admin it takes me back to the old site. The new site is a dev server where I plan to make style changes before I take the site live. What am I missing about this transition? How do I get the site to respond to the dev url correctly?
If this is a single WordPress install, there are a couple database entries with your old domain. Specifically, `siteurl` and `home` within `wp_options`. That said, if the dev URL is temporary, you can also set the following two constants in `wp-config.php`: define('WP_HOME', ' . $_SERVER['SERVER_NAME']); define('WP_SITEURL', WP_HOME . '/'); Provided that WordPress is installed in the root of your website.
stackexchange-wordpress
{ "answer_score": 25, "question_score": 17, "tags": "wp admin, site url, paths" }
Setup SMTP setting in wordpress My contact form in wordpress is not working. When the contact form is submitting, I get the following message > Message sending failed " ... the SMTP setting is not configured.. How can I make the Mail to send. I'm using the plugin Contact Form7. How can I set up the email so that it will send to the user...
Wordpress uses PHPMailer class and has an action hook that you can use to configure it. For example: add_action( 'phpmailer_init', 'cyb_smtp_config' ); function cyb_smtp_config( $phpmailer ) { $phpmailer->IsSMTP(); $phpmailer->Host = 'smtp.mailer.com'; $phpmailer->Port = 25; $phpmailer->Username = 'username'; $phpmailer->Password = 'password'; } Be sure that you need a custom SMTP connection instead of configuring the SMTP service in your hosting. If you are not sure, contact with your hosting provider.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "plugins, email" }
Exclude some categories from the post page I have created a page called "videos", this is set as the post page where all post should show up. That´s fine, but I want to exclude all posts from two of the sites categories. I tried this code, but it´s not working it´s the right page id and the right category id´s. function exclude_category($query) { if(is_page(41)){ $query->set('cat', '-42, -43'); } return $query; } add_filter('pre_get_posts', 'exclude_category');
Yes your code will fail, but not because the above answer, but because this isn't the page you ask for. As you wrote, you set the page "videos" to be the page where all the posts are shown. When a page is set as a blogpage, Wordpress will use home.php or index.php according to the template hierarchy to display the blogpage, not a page template. This page is regarded as the home page, so `is_home()` will return true on this page So, to exclude the categories, you need to use this conditional: function exclude_category($query) { if(is_home()){ $query->set('cat', '-42, -43'); } return $query; } add_filter('pre_get_posts', 'exclude_category'); Happy Coding, Kuchenundkakao
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "exclude" }
wp_list_categories and custom post types I'm confused. * I have a custom post type "asset". * I created posts, and they are all at `/asset/post-title` * I assigned categories to the custom post type, e.g. "patterns" * The categories page shows the number of posts in each category * If you look at the post meta for any of these posts in single view, it displays the category(ies) assigned to it. HOWEVER, it you click the category link in the post meta ("patterns"), I get a page "nothing found". If I assign the category to a **normal** post type, those will show of the category page. What did I forget? EDIT: I should also add that the LINK for "patterns" is `/category/patterns`
By default, category archives only show posts of the default 'post' type. The below code should get it working how you want. —via CSS Tricks function namespace_add_custom_types( $query ) { if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'post_type', array( 'post', 'your-custom-post-type-here' )); return $query; } } add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "custom post types, categories" }
How to modify how the_content outputs a link to an internal page I like the default add link dialog in the wordpress editor. IT allows for easy linking to internal pages. I need to modify the output of those internal links. Right now its outputting: <a href="page url" title="linked page title">my text</a> I need it to be: <a href="page url" title="linked page title" data-id="linked page id">my text</a> I need the linked page id to be set in a data attr. Is this possible with a action/hook Thank you
I'm in a hurry so don't have time to provide any code, pseudo or otherwise, but you could probably do this in the `the_content` filter by using a regex to find all links and for each one find the page/post ID using `url_to_postid` and then replacing as appropriate. Good luck!
stackexchange-wordpress
{ "answer_score": 0, "question_score": 1, "tags": "hooks, the content" }
show all custom posts types in home page Hi i am working on custom post types in wordpress presently we are using below code to display the custom posts, but the thing is the custom post types are keep on increasing, add_action( 'pre_get_posts', 'add_my_post_types_to_query' ); function add_my_post_types_to_query( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( 'post_type', array( 'books', 'recipes', 'movie' ) ); return $query; } from above query i am calling the posts from books,recipes and movie but due increase of post_types i cant add all the post types in the above query. dose anybody have idea how to call all the posts from all post_types.
add_action( 'pre_get_posts', 'add_my_post_types_to_query' ); function add_my_post_types_to_query( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( 'post_type', 'any' ); return $query; } refer to <
stackexchange-wordpress
{ "answer_score": 2, "question_score": 0, "tags": "custom post types, query, homepage" }
How to get URL param to shortcode? I can't find solution for this ... URL: _www.foo.com/some_page/?name=John_ Page content: > Hi [Name]! Worpress still redirects back to _www.foo.com/some_page/_ I tried everything ... I need something like the following in _functions.php_ function name_shortcode() { return $_GET['name']; } add_shortcode( 'Name', 'name_shortcode' ); And on another page: Click here _www.foo.com/some-page/?name=John_
If I understand your question correctly, you want to be able to get the parameter from the url, add it to the shortcode so you can add the parameter to the content. See if this works: add_shortcode('name', 'get_name'); function get_name() { return $_GET['name']; } In the wordpress backend editor you would have something like: Hello [name], Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
stackexchange-wordpress
{ "answer_score": 6, "question_score": 7, "tags": "shortcode, urls" }
post_prev & post_next within same category In my single blog view, I only want to show the Prior and Next buttons within the same category the current post is in. I thought I could just change these two lines thus (set first term to true). but that doesn't seem to ever display the buttons. Any suggestions ? $post_prev = get_adjacent_post(false,'',true) ? get_permalink(get_adjacent_post(false,'',true)) : false; $post_next = get_adjacent_post(false,'',false) ? get_permalink(get_adjacent_post(false,'',false)) : false; Thank in advance!
Add this in your functions file and it links all single posts within the same category add_action('loop_end', 'wpsites_nav_links', 25 ); function wpsites_nav_links() { if( !is_single() ) return; previous_post_link('<span class="single-post-nav previous-post-link">%link</span>', '&lt;&lt; Previous Post in Category', TRUE); next_post_link('<span class="single-post-nav next-post-link">%link</span>', 'Next Post in Category &gt;&gt;', TRUE); }
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "themes" }
Display recent posts on front page I am new to theme development in wordpress i am working on a theme, i have a front page on which i want to display only the most recent 4 posts of any category to be displayed my code for this is <?php $the_query = new WP_Query( ‘showposts=4′ ); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <article> <a href=”<?php the_permalink() ?>”><img src="<?php bloginfo('template_directory'); ?>/img/post-images/Adithi_Dinner_blog.jpg" class="border" alt="image" /><h1><?php the_title(); ?></h1></a> <p><?php echo substr(strip_tags($post->post_content), 0, 100);?></p> <!-- <?php the_content( 'Read the full post »' ); ?>--> </article> <?php endwhile;?> but it gives me error that unexpected "=", if i remove 4 from the showposts=4 than it shows all the posts on that page, kindly help me how i fix it, thanks
Try this, <?php $args_latest = array( 'post_type' => 'post', 'ignore_sticky_posts' => 1, 'posts_per_page' => 4 ); $the_query = new WP_Query($args_latest); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <article> <a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory'); ?>/img/post-images/Adithi_Dinner_blog.jpg" class="border" alt="image" /><h1><?php the_title(); ?></h1></a> <p><?php echo substr(strip_tags($post->post_content), 0, 100);?></p> <!-- <?php the_content( 'Read the full post »' ); ?>--> </article> <?php endwhile;?> <?php wp_reset_query();?> Please check if it works for you.
stackexchange-wordpress
{ "answer_score": 0, "question_score": -1, "tags": "theme development" }
Is it possible to call a template file inside wysiwyg editor? And how? Is it possible to call a template file, say for example **mycustomtemplate.php** inside the default Wordpress WYSIWYG editor? I had this content. My team (This is my content inside the WYSIWYG) then I would like to call a template file. When your inside a template file you would write <?php get_template_part('includes/page-team-list'); ?> So how could I do this on the WYSIWYG editor? Or I have to use some **PHP plugins** for these php codes to work on WYSIWYG?
PHP (unlike HTML) is not meant to be used as content markup language. It is _server–side_ language with too much access. It is very challenging to safely and securely treat PHP as part of content. For basic example — what if one of your authors writes PHP code in a post which amounts to "make me site's administrator"? While WP plugins implementing this do exist, this is almost never good idea, especially at multi–author site. The _typical_ way of handling more elaborate content in WordPress is via custom fields / meta boxes (/frameworks for both), although it usually falls farther away from WYSIWYG.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "templates, wysiwyg, get template part" }
Why does my delete_post hook get called twice Here is how I've implemented it: add_action( 'delete_post', array( 'MyClassName', 'delete' ) ); In the delete function I was using a $wpdb->insert command to see whether the function gets called. But I see that the $wpdb->insert command is called twice (inserts two rows in my table). Any ideas as to why something like this might happen? I've also attempted to use the before_delete_post hook since it doesn't really make a difference to me but I get the same outcome. EDIT: It seems as though the delete function is called for each entry in the wp_posts table for some reason. So if the post has 3 revisions, the delete function will be called 4 times (the original post + 3 revisions). This is really weird behavior.
I found my solution. The function you hook into delete_post (or probably any other similar hook) executes as many times as needed. Considering delete_post needs to delete the post and all of its revisions, it will always run more than once. In order to avoid having your function execute each time WordPress deletes a record from the database you can use did_action( $hook ). This function returns the number of times the hook executed. With this in consideration, we can fix our multiple-executions problem by placing this condition into our function: if (did_action('delete_post') === 1) { // execute code here }
stackexchange-wordpress
{ "answer_score": 2, "question_score": 0, "tags": "actions, customization" }
list posts of two post types in a single template I have two post types. One is `post` & other one is `event`. Both sharing same categories & tags. I am listing the posts by `archive.php`. Now I am listing the posts as general by two different templates like `archive.php` / `archive-event.php`. But now I want to list them in a single template with mixing the posts and order by date. So any one would help me in this regard.
As I already mentioned in my comments, you must use `pre_get_posts` to add custom post types to archive pages. One thing you must keep in mind here though, you have two archive pages here, archive.php and archive-event.php, so `is_archive()` is going to return true on both, which will alter both pages. You will need to exclude archive-event.php from your function. To do this, you'll need to make use of `is_post_type_archive()` to check and exclude that specific page For all the possible parameters you can use with `pre_get_posts`, go have a look at `WP_Query` function cpt_in_archive($query) { if ( !is_admin() && $query->is_main_query() && $query->is_archive() && $query->!is_post_type_archive( 'event' )) { $query->set( 'post_type', array( 'post', 'event' ) ); $query->set( 'orderby', 'date' ); } } add_action( 'pre_get_posts' , 'cpt_in_archive' );
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "custom post types, posts, wp query" }
Display an author's posts on his own author page I have a blog that publish articles of any philosophers.Each philosopher has a special page that includes information about him. I've created a post type called _philosopher_. I want to display the title of each philosopher's articles under his page, but because every philosopher has many articles, I only want to display 10 articles and show the other articles on another page.
You can simply use the default author.php page to do this. If you don't have one, make a copy of index.php and rename it author.php Now, use `pre_get_posts` to include your custom post type in the author pages. This code goes in functions.php function cpt_on_author_page( $query ) { if ( !is_admin() && $query->is_author() && $query->is_main_query() ) { $query->set( 'post_type', array( 'post', 'philosopher' ) ); $query->set( 'posts_per_page', 10 ); } } add_action( 'pre_get_posts', 'cpt_on_author_page' );
stackexchange-wordpress
{ "answer_score": 1, "question_score": 1, "tags": "posts, query posts" }
Page not loading content - Body archive - page I'm working on this page The problem Is that is not loading any content, altought it's english version does: link I'm not getting any console errors so can't find where the problem is. !enter image description here The only difference I see is that the spanish page has : <body class="page page-id-584 page-template page-template-tienda-page-php logged-in admin-bar woocommerce woocommerce-page customize-support"> and the english version has : <body class="archive post-type-archive post-type-archive-product logged-in admin-bar woocommerce woocommerce-page customize-support"> It is exactly the same page, just translated to english, any ideas what could be the problem?
Reset your permalink from admin site. you may get page if there is a conflation with permalink.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "posts, pages" }
Do we have to santise html passing into Javascript ? How? I am passing a `<ul><li></li><ul>` to jquery .html() method and it works. I am assigning the html to a varaible and passing to .html() method . As some security scans suggest me to use `esc_html` on that variable. But if i use it breaks the menu as it is not rendering html , just printing it as text. So my question is -Do i have to sanitize HTML when i am passing it to Javascript , which is written by me ?? How ?
No, in fact sanitizing the string you listed would remove the HTML. The sanitizing functions are meant for user inputted data like a form submission. You can't always trust the information they pass through. Sometimes hackers may input malicious code that would be executed on your site after submission if you did not run it through a sanitizing function first.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 1, "tags": "jquery, sanitization" }
My php code is not being executed I have a wordpress page that has this code in the text editor part of a web page: This page creates a cookie and houses a etst link that is only accessible to the holder of the cookie. &nbsp; Test Link (authentication required). &nbsp; Syntax: <span style="color: #444444;">setcookie(name, value, expire, path, domain);</span> <?php setcookie(accesscookie, testing1234, time()+86400, /, foo.example.com); ?> When I navigate to the page it does not create a new cookie. Can somebody tell me what I am doing wrong to create the cookie?
I have figured out how to make a cookie system per-page and not on a site wide setup: I setup a java script section in my webpage to create the cookie (the text editor). Here is the code that I used: <script type=”text/javascript”> <!– document.cookie=”AuthCookie=OpenSesame; Domain=example.com; path=/”; //–></script> Hope this helps somebody out who does not want a site wide cookie creation setup :) Thanks to: @Edd Smith his help and guidance. I eventually looked into alternative wasy to make a cookie and found some documentation at W3Schools.
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "php, pages, cookies" }
How to print partial URL after #? In WordPress, is it possible to retrieve the part of the URL after a `#`? I'm trying `echo the_slug();` but it's showing front slug. But I want after #tag For example: I'm searching and land on this page. And print last anchor text. < >> Output: test1 < >> Output: test-test2
First off, the "slug" is what it is by definition. There is no such thing as a "front slug". As you do not elaborate/know on how the anchors/hashes are generated on the server side, all I can give you is a JS solution (proof of concept): if (window.location.hash) { alert(window.location.hash.substring(1)); }
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "slug" }
Wordpress Page doesn't load the template selected I've created a new page called `Work` and I've added it on the navigation bar; after that I've created a file called `work.php` in which I want to style the page mentioned above. The problem is that the page doesn't show the new template created in `work.php` even if I selected it on the Wordpress graphic interface, but instead it shows the template of `index.php`. Does anyone know where the problem is? Thanks!
I realise this question is 3 years old, but I just solved what sounds like a similar situation. * I have a page with a slug **join** * I created **page-join.php** and my issue was that it wasn't being referenced * I resolved the issue by changing the Join page's **Page Attributes > Template** from what was selected to **Default Template** ![enter image description here](
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "pages, page template, template hierarchy" }
jQuery HoverIntent is not a function I am working on a site that uses jQuery for a number of functions but I am having a repeating error: > hoverIntent is not a function I have referenced similar questions on this forum and they almost universally say that it is due to multiple references to jQuery, however, I have carefully reviewed the code and the source code and cannot find any duplicate references. There is just this one in functions.php // Load jQuery if ( !is_admin() ) { wp_deregister_script('jquery'); wp_register_script('jquery', (" false); wp_register_script('jquery', ("js/jquery.dropmenu.js"), false); wp_register_script('jquery', ("js/jquery.dropmenu2.js"), false); wp_enqueue_script('jquery'); } I would really appreciate another set of eyes to help pinpoint and resolve this dilemma.
I discovered that the HoverIntent is optional for the drop menu plugin. I turned that off and everything is working correctly now.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "functions, jquery, errors" }
Have two (permalinks) urls for a same custom post type I have an specific situation which I´ll try to explain. In the website I developing right now we have fairs (event) and every fair has a profile page. Lets call ns_fair custom post type and its loaded with permalink ` Hi have a page with the permalink ` where I list the archive fairs with a custom WP_Query Loop. When I click a fair, loads in ` and I need to load in ` It´s the same custom post, just loaded with another permalink, for website structure purposes only. That´s possible? Any suggestion? Thanks in advance.
Hmmmm.... I'm not sure this is possible since the fairs are not located under the "past fairs" CPT. However, the URL structure shouldn't be too critical as long as the navigation on your site is setup in a way that makes sense. Is there any other reason you want this behavior other than personal preference?
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "custom post types, permalinks, custom post type archives" }
Remove specific product from shop in woocommerce I have product category named "Test_category" in my shop. Specifically for that i want to hide this all product from my shop, Only hide for some time not delete or set status as pending from admin. Regards.
Add below hook to your theme function.php file add_action( 'pre_get_posts', 'custom_pre_get_posts' ); function custom_pre_get_posts( $q ) { if ( ! $q->is_main_query() ) return; if ( ! $q->is_post_type_archive() ) return; if ( ! is_admin() && is_shop() ) { $q->set( 'tax_query', array(array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array( 'private-clients', 'charity'), // Don't display products in the private-clients category on the shop page 'operator' => 'NOT IN' ))); } remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); }
stackexchange-wordpress
{ "answer_score": 2, "question_score": 1, "tags": "plugins, woocommerce offtopic" }
How to keep &nbsp; non-breaking spaces in the visual editor? I often add `&nbsp;` between connecting words so they break down together to a new line in important text. Switching to visual tab then back to text tab removes these. What can I do to keep them and avoid TinyMCE removing them? I found this but not sure how to apply it to WordPress: < My ugly solutions would be using a different custom string between words__like__this then later replacing them to `&nbsp;` in PHP, before display. Or just typing it on the visual tab, which translates to `&amp;nbsp;` but then converting them with PHP.
This seems to do it: function allow_nbsp_in_tinymce( $mceInit ) { $mceInit['entities'] = '160,nbsp,38,amp,60,lt,62,gt'; $mceInit['entity_encoding'] = 'named'; return $mceInit; } add_filter( 'tiny_mce_before_init', 'allow_nbsp_in_tinymce' ); Also see: < Any improvement suggestions?
stackexchange-wordpress
{ "answer_score": 7, "question_score": 4, "tags": "tinymce, visual editor, text, line breaks" }
To whom do emails get sent via the Wordpress comments form? I am working on a Wordpress 3.9.1 site, specifically the comments form. I have a copy of the site on my server and I want to test the form in the following three ways: * Adding a new comment * Replying to a comment * Replying to a reply of a comment To do so, I want to be careful not to send many emails out to the site owner, and I want to be **very** careful not to send any out any email to site users. I see under site > admin that there are options to "E-mail me whenever": * Anyone posts a comment * A comment is held for moderation These are both unchecked. My question is whether there is anywhere else I should worry about emails being sent (eg if someone replies to a user, does the user get sent an email?)
Emails are sent to site admin`(settings->general E-mail Address)` when you check the email me option . No need to worry if you un-check that assuming you are not using any plugins that overrides.
stackexchange-wordpress
{ "answer_score": 1, "question_score": 1, "tags": "comments, comment form" }
Retrieve current and localized month instead of writing array of months in desired language, and doing a function to replace date('m') with localized month... I believe there should be some fast and easy way to echo current month, after all its Wordpress? I have define('WPLANG', 'hr');
global $wp_locale; echo $wp_locale->get_month( date( 'm' ) );
stackexchange-wordpress
{ "answer_score": -3, "question_score": 0, "tags": "date" }
What exactly do this function declared into functions.php file of a WP theme? someone can help me to understand what exactly do this function declared into the **functions.php** file of a theme? function exclude_featured_tag( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'tag__not_in', 'array(ID OF THE FEATURED TAG)' ); } } add_action( 'pre_get_posts', 'exclude_featured_tag' ); I am not into PHP but looking it I think that if the user is on the home page it modifya query object exluding the posts having a specific tag. Is it right? If it's right can you explain me the last line: add_action( 'pre_get_posts', 'exclude_featured_tag' ); I think that it is used to automatically call the previous function what a specific event happen...is it right? what event? can you help me to understand how exactly work? Tnx
The last line tells WP to run this function on the `pre_get_posts` hook. That hook is the ideal place to modify the main query that will be run in order to build the page. In this case, `exclude_featured_tag()` checks whether WP is loading the home page, and whether it's about to call the main query for the page (as opposed to any other secondary queries being run). Then it modifies the query (`$query->set()`) to NOT include any post that has tag of `ID OF THE FEATURED TAG`. For more on the `pre_get_posts` hook, see the codex.
stackexchange-wordpress
{ "answer_score": 3, "question_score": 0, "tags": "php, plugin development, functions, theme development, customization" }
What file have I to create in my custom WordPress theme to show all the post belonging to a specific category? I am pretty new in WordPress theme development and I have the following doubt. I am working on this custom theme: < Into this website upper horizontal main menu I have a link named **LEGACY-POSTS** (the last one) that should show the list of all the posts belonging to a specific category (named **legacy-posts** ). Clicking on this link I obtain a white empty page: lnx.asper-eritrea.com/category/legacy-posts/ I think that it could depend by the fact that maybe I have to implement this page. Is it right? I was looking on the **Template Hierarchy** official WordPress documentation page: < But I am finding some difficulties to understand what name I have to use for the file that implement this page. Can you help me? Tnx
A custom template for one specific category should be named `category-slug.php` where "slug" is the WP slug for the category. WordPress will automatically use this file to display the archive for that one category. You could also name it using the ID of the category. If your category is "legacy-posts", then your template file should be named `category-legacy-posts.php`.
stackexchange-wordpress
{ "answer_score": 2, "question_score": 0, "tags": "php, posts, theme development, loop, customization" }
How can I access my posts on my wordpress site, now I've lost the domain name for it? I wrote some posts for a wordpress site I've got, unforunately I couldn't renew the domain name in time, so I want to access the text in those posts, and put it into my new site, how can I do that? I can access the site via my cpanel and IP for the site, and I can see the WP files there but I don't know where the post data is?
The posts in WordPress are stored in a `MySQL database`. There are several ways to access a `MySQL database`, but the most common way is that your web host provides access to a tool called `PHPMyAdmin`. This is a web application that allows you to manage your `MySQL database`, including making an `SQL dump`. An SQL dump is a file with the data of your `MySQL database`. It is possible to import the `SQL dump` in your new database. Basically, when you install WordPress on your new domain, you should be able to reset your new database and import your `SQL dump` again. For information on how to do this, contact your web host - some use other software than `PHPMyAdmin`. Good luck!
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "posts" }
Show number of posts AND number on current page I know how to show the total number of posts in a site, but is there a way to show the number of posts the page you're on is displaying? For example, I want to show this at the bottom of each of my blog pages (8 posts per page); Index - "Showing posts 1 through 8 of 294 total" Page2 - "Showing posts 9 through 17 of 294 total" Page3 - "Showing posts 18 through 26 of 294 total" The posts are displayed in chronological order if that makes a difference. Is this even possible?
This should work, but I haven't tested it: global $wp_query; $page = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $ppp = get_query_var('posts_per_page'); $end = $ppp * $page; $start = $end - $ppp + 1; $total = $wp_query->found_posts; echo "Showing posts $start through $end of $total total.";
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "posts, pagination" }
Is there a way to add flv video to video.js player in Wordpress? Video.js supports mp4, wemb and ogg formats but i think there is a trick to add flv format example: <
You can directly add flv video to any post/page by using Add Media button. If you want to add a video manually (I mean directly using link of video), then you can use [video] shortcode. This video shortcode supports 'flv' format. WordPress Codex documentation of video shortcode explains more about it.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "video player" }
Alternative to query->set that appends conditions instead of overriding I am applying a taxonomy condition to the main query in my plugin which is excludes an array of post formats from it. It's being done as follows: $query->set('tax_query', array( array( 'taxonomy' => 'post_format', // Post formats are queried as taxonomies 'field' => 'slug', 'terms' => self::$formats, 'operator' => 'NOT IN' ) )); This is working - the post formats are being excluded as I wanted them to. However the problem is that if any `tax_query` condition was specified before, then it would be overridden and lost in space and time. Is there any way I can append my `tax_query` condition without losing overriding all conditions before it?
`$query->set` isn't the only method. What you want is `$query->get`. Using `get` you can retrieve your existing value, append the additional part you wanted, then set the query variable with the new argument $tax_query = $query->get( 'tax_query' ); // add the extra parameter $query->set( 'tax_query', $tax_query ); The adding the extra parameter is just appending another item using standard PHP array functionality.
stackexchange-wordpress
{ "answer_score": 2, "question_score": 1, "tags": "wp query" }
What is best practice for writing/using custom PHP classes in a theme? I want to use a custom PHP class in my theme, however I am not sure where to add it. Is there best practice used by the community for including PHP classes with a theme or is that an individual developer's decision? The first class I want to add is a custom menu Walker, however there will be more, and they will be tied to my theme. I am split between creating them in separate PHP files versus adding them to functions.php - or maybe there is any other ways please? Many thanks in advance for your input. Katrina
Ultimately it's about maintainability and readability. Functions.php is good for adding just that - functions. Small snippets of code that modify or add to the theme. When you start getting into classes or building some kind of framework, you need to start separating your concerns whether that be moving them into a plugin or simply including your class files into the theme and keeping a well organized directory. And there's no performance to contend here - it's all about what makes development the easiest and most maintainable for you.
stackexchange-wordpress
{ "answer_score": 4, "question_score": 3, "tags": "theme development" }
Issues with Wordpress permalinks I am new to wordpress but have experience in Drupal. I am developing wordpress site which I access as `localhost/site1`. I created another copy of it to experiment a few things. I can access another site as `localhost/site2`. The strange thing I see now is whenever I click on a post from `localhost/site2` it takes me to `localhost/site1`. For example I am taken to `localhost/site1/my-post` from site2 when expected is `localhost/site2/my-post`. Then I checked the database and saw the links were stored in posts table in guid column. I was expecting only relative URLs to be saved in tables(as in drupal) but here the full URL is saved in database. How can I make all URLs as `localhost/site2/my-post`? Also how can I make this work when I move it online to a particular domain ?
This is a typical issue when moving WordPress sites. WordPress stores URLs - both main configuration URLs and resource URLs - in the database. These must be changed in the database when moving to a new domain or mail URL in localhost. Don't use simple SQL change queries in the database via adminer or phpmyadmin. WordPress and some themes store configurations and URLs in serialized data, which a simple SQL query won't correctly handle. See Moving WordPress « WordPress Codex for full details on moving Wordpress and WordPress Serialized PHP Search Replace Tool to correctly find/replace URLs in the database.
stackexchange-wordpress
{ "answer_score": 2, "question_score": 0, "tags": "permalinks, urls" }
Showing a link with Javascript based on log-in status <?php if ( is_user_logged_in() ) { echo ''; } else { echo '<a onclick="ppOpen('#login_panel', '800',1)" href=\"javascript:\">Entrar</a>'; } ?> Hello, I'm trying to make this work but i'm not sure how to do the syntax.... any help would be great! thanks!
It is string escape problem with PHP Syntax Correct Code: <?php if ( is_user_logged_in() ) { echo ''; } else { echo '<a onclick="ppOpen(\'#login_panel\', \'800\',1)" href="javascript:void(0)">Entrar</a>'; } ?>
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "menus, javascript" }
What's the point of using Wordpress's built in admin-ajax.php? I'm trying to understand why using admin-ajax.php is advantageous over doing something like this: add_action ('wp_loaded', 'my_ajax'); function my_ajax() { // Do Ajax, Check $_POST die(); } With the JQuery ajax just pointing to the blog's index. admin-ajax.php seems rather convoluted to do something simple, with having to register scripts and add hooks and set up a js variable url to point to the admin-ajax.php and then with wp_ajax_nopriv_ vs wp_ajax_. Not to mention it's kind of weird that back end is being mixed with the front end. I'm pretty sure there's an analogy there. So why use it? Does admin-ajax.php have less overhead? Is it purely a standards thing? Is there something magical about it? What do you miss out by not using it?
First, like many things in WordPress, it's not like someone decided "let's make this neat and convenient". It was more like someone slapped it together for something, then it got used more in admin, then it got used a lot in admin, then it became kind of practice to use it for non-admin as well. Or something along these lines. Second, it's not as much _what_ it does, as _knowing_ that it always does same thing. You have _no_ idea what request to the arbitrary part of front end is doing. What if analytics plugin is running, counting it as page view? What if there is complex redirect logic that handles seasonal URLs? What if, what if... Ajax endpoint is _meant_ for ajax: * it declares `DOING_AJAX` constant for context, many things look for it to skip doing whatever they are doing * it sets up HTTP headers and stuff * it gives you basic code organization convention, including user/anonymous distinction
stackexchange-wordpress
{ "answer_score": 4, "question_score": 2, "tags": "plugins, functions, jquery, admin, ajax" }
wordpress blog in subdirectory or subdomain I'm using Wordpress for my website. Now I'd like to add a blog section to my website. I'd like it to be accessible like this: < or even better, as a subdomain: < I've done some research and my understanding is that the second option would be possible using a multiple-site configuration. I've tried and I broke everything :) Otherwise I could just install a new wordpress instance but it's going to be tedious. So the first option seems more feasible but I'm not sure how to implement it ?
The first option (/blog/) is the easiest: * create a new blank page called "Blog" * navigate to "Settings > Reading" and choose this new page as the value in the "Posts Page" drop-down Now, when you navigate to the new "Blog" page (which should be at /blog/ unless there is something unusual about your setup), all Posts are displayed, the most recent first.
stackexchange-wordpress
{ "answer_score": 3, "question_score": 1, "tags": "directory, blog" }
Show menu only if_page is I have created 4 menus (submenus) in wordpress. I need to know how show this menus only if pages ids are "10 and 11 for example. Is possible? I'm desperated after two hours... thanks! This is my code for one of them: <?php if( has_nav_menu( 'smone-menu', 'responsive' ) ) { ?> <?php wp_nav_menu( array( 'container' => '', 'menu_class' => 'smone-menu', 'theme_location' => 'smone-menu' ) ); ?> <?php } ?>
Have you tried adding the is_page() conditional like this: if( has_nav_menu( 'smone-menu', 'responsive' ) && is_page(array( 10,11 ))) {
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "menus" }
Weird Parse Error on Uploaded File so I'm working on this theme on my localhost, and I just uploaded it to see if it'd work and I'm getting a weird parse error for these lines in my functions.php file. It works fine on my localhost though: $next = isset($_POST['next']) ? $_POST['next'] : false; $prev = isset($_POST['prev']) ? $_POST['prev'] : false; $nexp = explode('=', explode('?', $next)[1])[1]; $pexp = explode('=', explode('?', $prev)[1])[1]; It says, unexpected '[' on the line starting with $nexp and $pexp. I'm a bit confused (and tired), and I don't really see what the problem with this line is, and why it would work on localhost but not on the server. Any help would be appreciated.
Array dereference (accessing array item directly after function result) is only possible since PHP 5.4, I'm guessing your remote server is running a lower version. Just use: $varname_temp = explode( 'stuff' ); $varname = $varname_temp[1]; Also on a side note, it's always best to set your local to use the same PHP version as your remote server. Kat
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "errors" }
Retrieve data from post meta meta_key having '_product_fq_image' while having author_id and post type is PRODUCT How can we retrieve data from postmeta having '_product_fq_image' while having author_id and post type is PRODUCT I have tried this $lt_content_design= get_user_meta( $user_id, 'lt_content_design', true ); but how can we use JOIN here for listing only particular user data having posttype is product
To get post meta fields, you can use `get_post_meta()` function, but obviously not `get_user_meta()`, the name of the functions are self-explanatory. The first parameter of `get_post_meta()` function is the post ID; here you define which post you want to get postmeta from. So, you need to get frist the post ID of the post you want and then pass this ID to `get_post_meta()`. For example, you can get the posts from author with ID 5, limit to "product" post type and then get the post meta for each found post. For example, using `get_posts()`: $args = array( //Get all posts from author 5 and post type "product" 'author' => 5, 'post_type' = 'product', 'posts_per_page'=>-1 ); $posts = get_posts($args); foreach($posts as $post) { get_post_meta($post->ID, 'lt_content_design', true); }
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "posts, users, post meta" }
Get XAMPP to divert to my site automatically When I enter my domain name into the web browser, eg. www.mysite.com, xampp automatically diverts to www.mysite.com/xampp How can I change it to go directly to my site folder eg. www.mysite.com/mysite Thanks and Regards
The problem seems to be with Apache configuration. Please check your httpd.conf file and check also if any virtual hosts (v-hosts) are defined. More details on the httpd.conf file: <
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "xampp" }
Select a specific post to display in a news box I'm building a website based on posts (kind of blog platform). I would, however, like to be able to choose which post, from any category, to display in the homepage in a news box system. All the plugins/ themes I've been trying automatically display them from the most recent or only one category or tag. The problem is, I'd like to be able to choose a particular post, from any category. Is there a way to achieve this? If anybody can give me a suggestion, it will be wonderful!
Why not simply make any post sticky using the visibility settings in the Publish box located on all Edit Post screens and add styling to make it look like a news box. !enter image description here
stackexchange-wordpress
{ "answer_score": 1, "question_score": 1, "tags": "posts" }
How to prevent people from seeing an attachment to a scheduled post that's released in future? I'm developing a website for a large corporation. They release financial documents on specific dates and times. I've developed a custom form to upload these pdfs to articles, and custom loops that display only these attachments on specific pages. When the date of the article is set in the future, the attachments are released when they should and the system works pretty well. The only thing is, that for the company it's super-important that there is no access to the attachment-files before the date when it's supposed to come out. So if anyone can guess the name of the attachment they can just go to wp-content/uploads/financialdocument10.pdf and read it before it's supposed to come out. Is there any ideas how to prevent this? My only idea so far is to add some random characters to the file name so it's impossible to deduce it's name on the the basis of previous filenames. Thanks!
WordPress is engineered exclusively to reside in web-accessible folders, unlike many less user-centric web frameworks, which separate locations of private (code, etc) and public (assets). Making filenames hard to guess is about as good as it gets with normal workflow. To have this _really_ tight you would need completely custom workflow with files places into web-inaccessible directory and moved to public ones at the release time.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "posts, attachments, scheduled posts" }
How to make pages metabox display all hierarchy in menu admin? Running WP 3.9.1 I've got lots of pages and sub pages. When I get to create menu lots of sub pages doesn't display on the right level, but some do? This is how the pages metabox is displayed The pages metabox should display something more like this * * * **_EDIT_** Number in screenshot are a bit screwed sorry about that. The problem is after the first few all the pages are displayed on the same level but they shouldn't. It should look like this * Page 1 * SubPage A of Page 1 * SubPage B of Page 1 * SubPage 1 of SubPage B * SubPage 2 of SubPage B * SubPage 3 of SubPage B * Page 2 * SubPage A of Page 2 * SubPage 1 of SubPage A * SubPage 2 of SubPage A * SubPage B of Page 2 * SubPage 1 of SubPage B * SubPage 2 of SubPage B ... * * * Any idea what cause this or how to fix it, so i can manage my menu?
Found a fix. Seems pagination is breaking the display of the the Pages hierarchy in the metabox. Here is the fix to disable page pagination in the nav menu metabox < Thanks
stackexchange-wordpress
{ "answer_score": 1, "question_score": 1, "tags": "menus, pages, admin menu, sub menu" }
Click thumbnail, change main image - NextGen or somethign else? I'm trying to get the main image to change depending on the thumbnail image clicked. I would want the main image to take its new src from the a tag of the thumbnail that was clicked. Good example is Gallery in 4 Lines but I'm looking for some kind of an existing solution (plugin) for Wordpress. Not sure if NextGen can do that. Thanks
The NextGEN Pro Horizontal Filmstrip Gallery display with caption option will display basically the same output as what you are referring to in the other example.
stackexchange-wordpress
{ "answer_score": 1, "question_score": -1, "tags": "gallery, thumbnails, plugin nextgen gallery" }
Wrong category being displayed in URL I'm a newbie to wordpress so please bear with me. I have two categories I'm using, basketball and baseball. I've only recently added the baseball category and if I add a new post with category baseball, in the URL, the post displays /basketball/post-name regardless of what I do. I've changed the permalinks settings and played with some plugins as well, and I've checked to make sure my SEO plugin isn't messing with these things. Thanks for any help.
just an update: I used the plugin WP Permastructure to solve the issue by adding /%category%/%postname%/ in the "category" section on the bottom of the permalinks settings page (the permalink adds new fields). Not really sure why this works but changing the permalink structure normally does not.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "categories" }
.htaccess regex for redirect subdomain to root domain & redirect old permalinks ok, i have at site a blog.mysite.com, and recently migrated all content to a new wp install at mysite.com. i want to redirect all of the posts from blog.mysite.com to mysite.com and also account for permalink structure. blog.mysite.com is /%year%/%monthnum%/%day%/%postname%/ mysite.com is /%category%/%postname%/ the goal is if anyone goes to blog.mysite.com /%year%/%monthnum%/%day%/%postname%/ they will be redirected to mysite.com is /%category%/%postname%/ i want to ultimately delete all content at blog.mysite.com as well, so i wound want to make this work via .htaccess after it's deleted. i can't seem to work out exactly how to do this in .htaccess. also, both sites live in their own directories on the server. /blog.mysite.com and /mysite.com thanks so much for your help!
this was the answer via htaccess (to be placed in htaccess within the blog.mysite.com directory). RewriteEngine On RewriteCond %{HTTP_HOST} ^(?:blog\.)site\.com$ [NC] RewriteRule ^ [L,R=301]
stackexchange-wordpress
{ "answer_score": -1, "question_score": 0, "tags": "permalinks, redirect, htaccess" }
$args Orderby The number I have this code $cat_args=array( 'include' => '42,210,21', 'orderby' => '42,210,21', 'order' => 'asc' ); $categories=get_categories($cat_args); foreach($categories as $category) { $args=array( 'showposts' => 1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1 ); I want the `orderby` to be the same order of `include`, not by name or id
You could use the `get_terms_orderby` filter: add_filter( 'get_terms_orderby', 'wpse156153_get_terms_orderby', 10, 3 ); $categories=get_categories($cat_args); remove_filter( 'get_terms_orderby', 'wpse156153_get_terms_orderby', 10, 3 ); with function wpse156153_get_terms_orderby($orderby, $args, $taxonomies ) { return 'FIELD(t.term_id, ' . $args['include'] . ')'; }
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "categories, wp query, custom field, order" }
Add custom classes to anchor in wp_nav_menu I want to add a custom class to anchors in `wp_nav_menu` outputs. Default for example is: <li id="menu-item" class="menu-item menu-item-type-custom"> <a href=" </li> I want this : <li id="menu-item" class="menu-item menu-item-type-custom "> <a href=" class="class">example</a> </li>
You can do this with the `nav_menu_link_attributes` filter. add_filter( 'nav_menu_link_attributes', 'wpse156165_menu_add_class', 10, 3 ); function wpse156165_menu_add_class( $atts, $item, $args ) { $class = 'class'; // or something based on $item $atts['class'] = $class; return $atts; }
stackexchange-wordpress
{ "answer_score": 26, "question_score": 10, "tags": "theme development, menus, themes, hooks, navigation" }
Frontend Form for Custom Post Type I want to add frontend post to custom post types. Till now I have figured out how to do it but only to the regular post type(not custom post). I am using this code. ( _Couldn't paste it here hence please find it atPastebin_) Need to know what code can be added to make the code applicable to custom post type(such as "books" or "places")
Have you tried changing Line 83? Change `'post_type' => 'post', to 'post_type' => 'YOURCUSTOMCPT',` See Codex article for `wp_insert_post` for reference - codex.wordpress.org/Function_Reference/wp_insert_post
stackexchange-wordpress
{ "answer_score": 2, "question_score": 2, "tags": "custom post types, front end" }
Swatch Internet Time for article timestamps The date and time when the article was published is shown below the article title. How do I show Swatch Internet Time there instead of local time?
You can use `'B'` as the format for `the_date()` and/or `the_time()` to generate Swatch Internet Time format. If you're modifying a theme, just find the references to `the_date()` and/or `the_time()` in the template files and change the value of the `format` parameter. If you're creating a plugin, you can hook into the `get_the_date` and/or `get_the_time` filters.
stackexchange-wordpress
{ "answer_score": 2, "question_score": 1, "tags": "timestamp" }
Shortcode in Text Widget not working Not sure why this is not working. I get no errors, but just shows nothing at all in the text widget. The shortcode works on pages and posts. WordPress version 3.9.1 add_filter('widget_text', 'do_shortcode'); // Shortcode for Widget copyright Year [year] function copyyear_shortcode() { $year = date('Y'); return $year; } add_shortcode('year', 'copyyear_shortcode'); Any help would be greatly appreciated.
Try switching to a different theme (such as 2012) and back to this theme again. Some times caching plugins and some hosting environments can cause issues.
stackexchange-wordpress
{ "answer_score": 0, "question_score": 1, "tags": "widgets, shortcode" }
Get multiple term objects by ids I have an array of ids that correspond to terms. How do I get the full term objects for each id? $ids = array(1,2,3); Ideally, I would love to be able to do something similar to the way taxonomy queries with `WP_Query` work, but obviously with terms instead of posts.
I wonder if you mean something like this modified Codex example: // Fetch: $terms = get_terms( 'category', array( 'include' => array( 1, 2, 3 ), ) ); // Output: if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { $li = ''; foreach ( $terms as $term ) { $li .= sprintf( "<li>%s</li>", $term->name ); } printf( "<ul>%s</ul>", $li ); } where `$terms` contains an array of term objects, empty array or the `WP_Error()` object. Check the Codex on `get_terms()` to get more information on the _output_ and the _input_ arguments. There you can get more code examples.
stackexchange-wordpress
{ "answer_score": 4, "question_score": 2, "tags": "custom taxonomy, terms" }
Root level category, tag, author and archive pages to work I want root level Wordpress urls to work for category, tag, author and archive page. For example right now categories pages are. domain.com/category/cat-name/ it shows posts under cat-name category. But if someone visits domain.com/category/ it shows 404 error, page not found. I want all these URLs to show some unique content created specifically for each link. domain.com/category/ domain.com/tag/ domain.com/author/ domain.com/archive/ How can we do that?
It is not possible by default. Your only possible way out of this is to create custom **page templates** which you can modify to display something like an index or any custom content When you create a new page in the back-end, choose this custom template, and make sure that your page slugs is named accordingly so that when you visit > domain.com/category/ that this custom page template is being displayed
stackexchange-wordpress
{ "answer_score": 2, "question_score": 2, "tags": "categories, permalinks, tags, archives, author" }
Where is the order of a menu saved in the database I want to use a custom menu from the wordpress database but I get the elements disordered and I want them to have the same order how can i do that? There is a place in the database where the order is stored?
You will get the nav_menu_items from `wp_term_relationship` in correct order. if you want to confirm there order by knowing there order number... u will get the object id of each item from the previous table and search for that id in `wp_posts`. then there will be column for that particular row named `menu_order`. so that u can get the actual order number.
stackexchange-wordpress
{ "answer_score": 2, "question_score": 0, "tags": "menus, database, order" }
Folder Name and Category Name Wordpress I have URLs that need have the old URL for download files. Before, it has 2 separate website. The MAIN and the PORTAL website. I want both websites under in one admin page. So I did and make "PORTAL" as CATEGORY and it works well. The problem is, I have "404 Not Found" page whenever I tried to view the PDF files. The files are now in: website/wp-content/uploads/2013/01/31/ before: website/portal/wp-content/uploads/2013/01/31/ Since I made PORTAL as category, I deleted the "PORTAL" folder. How can I make the URL path or what to change in the admin page or in .htaccess file to still have the OLD url? I cannot change the URL to the new one since most of the files are link to other websites.
## Option 1 This is the clean way using .htaccess. Put this at the top of your .htaccess: <IfModule mod_alias.c> RedirectMatch 301 /portal/wp-content/uploads/(.*) /wp-content/uploads/$1 </IfModule> ## Option 2 This is a bit brute-force and I therefore wouldn't recommend it, but I include it here for completeness. Just copy all files in `website/portal/wp-content/uploads` to `website/wp-content/uploads/` manually
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "permalinks, url rewriting, htaccess, 404 error" }
if conditional in sidebar plugin Password content shortcode I have this code [][/] on page and work perfectly! so i want now if i can in sidebar check if the form is properly completed on the page and then will echo some word in sidebar. for example something like this in sidebar: if (isset($_POST['csp_submit'])) { if ($_POST['csp_input'] == $password AND $password != '') { echo 'password is corect and echo some words'; } else { echo 'password is wrong'; } } else { echo 'you must enter pasword on page'; } <
Two ways you can do that, 1. You can write a new short code for e.g [check_sumbit] and then use it in side bar, and that short should check if form is submitted correctly 2. You can use php code as you mentioned in your question using some plugins that allow php code in widgets. For e.g. <
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "php, sidebar" }
Is there a way for a plugin to add an attribute to the <html> tag of a theme? I'm currently working on an appcache plugin for WordPress. One thing that it should do is add a manifest reference in the `<html>` tag of the site. It has to resemble something like this: `<html manifest="manifest.appcache">` Is there any way of doing this programmatically within a plugin? My current idea would be to identify the `<html` part of a theme's header file and inject the manifest using something like `str_replace()`. However, I can't see a way of filtering the output of the `get_header()` function either via the `get_header()` action or `load_template()` function. If anyone has any ideas I'd appreciate help.
You can probably use the `language_attributes` filter (from the `language_attributes()` function) to add it. It should receive an output like `lang="en"` and you can add to it before printing to the `<html>` tag: add_filter( 'language_attributes', function( $attr ) { return "{$attr} manifest=\"manifest.appcache\""; } ); or without a anonymous function add_filter( 'language_attributes', 'wpse140730_add_manifest_to_language_attributes' ); function wpse140730_add_manifest_to_language_attributes($output) { return $output . ' manifest="manifest.appcache"'; }
stackexchange-wordpress
{ "answer_score": 6, "question_score": 5, "tags": "php, plugin development" }
User agent stylesheet distorting site I am creating my first wordpress site from its html version. My html site is quite simple and seems to be looking correct in popular browsers. You can see it now at k-gayduk.ru. In my every day life I use Mozilla so I used it in this work too. Everything was fine: my wp site was looking exactly as its html copy. However, when I checked other browsers I found that user agent stylesheet distorted it in Google Chrome and in IE8 to very high extent as well. This means that wp influenced css styles in this specific way. I googled the problem, but found nothing except reset.css suggestion which was useless in the case. However I think I am not the first one facing the problem. Appreciate any help on the matter. I should say sorry as not able to show wordpress site because it is still on localhost.
You could try adding a CSS reset to your css. I use Normalize while developing WordPress themes.
stackexchange-wordpress
{ "answer_score": 0, "question_score": -1, "tags": "users" }
Add sidebar in inner pages I am new to wordpress coding. I want to add a sidebar to pages other than the home page, both pages are my custom templates. I want to list in admin so that admin can edit the content of sidebar. Also, do I need to create widget to display every side bar?
This question has been answered many times already on this site. There's 2 simple steps: **One:** Add to functions file register_sidebar( array( 'name' => __( 'Your Sidebar' ), 'id' => 'wpsites-sidebar', 'description' => __( 'Your Sidebar Description.' ), 'before_title' => '<h1>', 'after_title' => '</h1>', ) ); **Two:** Add where you want to output sidebar <?php if ( is_active_sidebar( 'wpsites-sidebar' ) ) : ?> <ul class="wpsites-sidebar"> <?php dynamic_sidebar( 'wpsites-sidebar' ); ?> </ul> <?php endif; ?> For further learning, look in the Twenty Fourteen default theme to see how its done. `register_sidebar` `dynamic_sidebar`
stackexchange-wordpress
{ "answer_score": 0, "question_score": 0, "tags": "widgets, sidebar" }
Retrieve options set through a plugin I am creating a simple "global settings" plugin, which simply saves a few text fields. Saving works as intended, and I can retrieve my data in frontend as follows: <?php $options = get_option('wpglobalsettings'); ?> <?php echo $options['wpglobalsettings_image1']; ?> What I would like though, would be to avoid having to call get_option('wpglobalsettings'); on each page I plan to use my plugin on. So I was thinking I could add my $options to an action hook. I did the following, but it doesn't work ( I get an empty array. function wp_global_settings_frontend(){ $options = get_option( 'wpglobalsettings' ); } add_action('the_content', 'wp_global_settings_frontend'); Can You help? Many thanks!
First of all, the_content is a filter hook and not action. Secondly, it will only work when post content will be access on your site front end. You can probably make use of global variables instead. e.g. $options = get_option( 'wpglobalsettings' ); and then use wherever you want to access as follows - global $options;
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "plugins, actions" }
How to stop Wordpress Search form from searching only in current page? My wordpress search form (code below) appends the query string to current page only. For example if I go to ` and then use the form to search for some content the query string looks like this ` and only searches within the current page. It should be searching from the root like this: ` How do I fix this? <form class="form-inline" role="search"> <div class="form-group"> <label class="sr-only" for="search">Search:</label> <input type="text" name="s" class="form-control" placeholder="Search"> </div> <button type="submit" class="btn btn-default">Submit</button> </form>
When you omit the `action` attribute from the `form` tag, it defaults to the current URL. Give an explicit URL for the form's action to direct it to a specific URL- <form class="form-inline" role="search" action="<?php echo home_url( '/' ); ?>">
stackexchange-wordpress
{ "answer_score": 1, "question_score": 1, "tags": "search, query string" }
Restrict access to xmlrpc.php I am fairly new to Wordpress and I recently noted a huge amount of traffic hitting the following: 162.242.170.222 - - [01/Aug/2014:08:18:54 -0500] "POST /xmlrpc.php HTTP/1.0" 503 4859 162.242.170.222 - - [01/Aug/2014:08:19:01 -0500] "POST /xmlrpc.php HTTP/1.0" 503 4859 162.242.170.222 - - [01/Aug/2014:08:19:01 -0500] "POST /xmlrpc.php HTTP/1.0" 503 4859 162.242.170.222 - - [01/Aug/2014:08:19:02 -0500] "POST /xmlrpc.php HTTP/1.0" 503 4859 162.242.170.222 - - [01/Aug/2014:08:19:11 -0500] "POST /xmlrpc.php HTTP/1.0" 503 4861 162.242.170.222 - - [01/Aug/2014:08:19:13 -0500] "POST /xmlrpc.php HTTP/1.0" 503 4861 162.242.170.222 - - [01/Aug/2014:08:19:18 -0500] "POST /xmlrpc.php HTTP/1.0" 503 4861 I blocked the IP address which is a temp solution. Is there a permanent solution to blocking these requests, expecially if I don't need
There are plugins for that: e.g. < You can also write a filter yourself add_filter('xmlrpc_enabled', '__return_false'); You can simply add this code your theme functions.php (located in wp-content/themes/your_theme). However, you are advised to create a child theme (< so that your modification does not disappear when you update the theme. Alternatively, you can create your plugin (< where you will put all your WordPress tweaking. I also add the following for a better protection: /** * Secure WordPress by removing version */ remove_action('wp_head', 'wp_generator'); /** * Secure WordPress by hiding login errors */ function hide_login_errors($errors) { return 'login error'; } add_filter('login_errors', 'hide_login_errors', 10, 1);
stackexchange-wordpress
{ "answer_score": 4, "question_score": 2, "tags": "xml rpc, hacked" }
What is the wordpress Git hub Repo master on beta? I am fairly new to Wordpress and have started checking out the latest version from Github. I assumed that the latest version would be master but it seems the latest is 3.9-branch 1. Is the 3.9 branch the correct one to use? 2. Can I revert to the production version if I have installed beta on a site? 3. Is the Github version of Wordpress always up to date?
I am not sure what gave you the idea 3.9 is latest, looking at Branches · WordPress/WordPress it hadn't had commits in 3 months. 1. Correct for what? If you want trunk then use master branch from GitHub. 2. Typically you can, but it's matter of probability and not guaranteed. WP won't roll back any upgrade changes (such as database schema changes) on downgrade, but they are rarely breaking in such scenario. 3. Yes, it is automatically updated and receives latest commits in minutes after SVN.
stackexchange-wordpress
{ "answer_score": 1, "question_score": 0, "tags": "github" }