I have been thinking about developing my own theme framework for worpdress. I'd like to use jquery ui to build a bootstrap 3.0 drag and drop interface, which I already have worked out, but I can't figure out how to edit the "Pages Add New Screen" as referenced here: https://codex.wordpress.org/Pages_Add_New_Screen
Would I add files to my client side theme that affected my admin structure as well? Does anyone have any suggestions as to how to do something like this. Alot of themes these days come with these drag and drop frameworks and it would be nice, to be able to create one of my own, just need some direction on where to start editing / looking.
We add a Custom Meta Box and do our thing inside it.
There are some hooks that are not Meta Boxes and we can use to insert content into that admin page:
add_action( 'edit_form_top', function( $post )
{
echo '<h1 style="color:red">edit_form_top</h1>';
});
add_action( 'edit_form_after_title', function( $post )
{
echo '<h1 style="color:red">edit_form_after_title</h1>';
});
add_action( 'edit_form_after_editor', function( $post )
{
echo '<h1 style="color:red">edit_form_after_editor</h1>';
});
add_action( 'edit_page_form', function( $post )
{
// edit_page_form is ONLY for pages, the other is for the rest of post types
echo '<h1 style="color:red">edit_page_form/edit_form_advanced</h1>';
});
add_action( 'dbx_post_sidebar', function( $post )
{
echo '<h1 style="color:red">dbx_post_sidebar</h1>';
});
The widget_text block belongs to Advanced Custom Fields (it's a repeatable/sortable field). I'm not sure anymore, but I think it removes the Meta Box borders with CSS or jQuery.
Instead of messing with the core files of wordpress (which would be a really bad idea) I would suggest making a custom field/sidebar or a menu page to use the drag/drop function to make a page or post.
http://codex.wordpress.org/Custom_Fields
http://codex.wordpress.org/Function_Reference/add_menu_page
If you want some examples on how others have done something similar (as a plugin) you could look at the code from others and see if you can use similar techniques to add additional fields within the screen options.
http://simple-fields.com/
http://www.advancedcustomfields.com/
Related
I'm new to jquery and php and trying to create my first Wordpress Plugin.
The plugin simply adds a metabox to posts which has a date field on the backend.
Metabox and field all added ok and appear when I go to create or edit a post.
However, I can't get the datepicker to work. Nothing happens when you click on the input box, the date selector does not show. Although the input box will only let me input numbers not letters.
The plugin is a single page of php code.
The top of my plugin file attempts to load the jquery:
function ca_load_jquery_datepicker() {
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css');
}
add_action( 'admin_enqueue_scripts', 'ca_load_jquery_datepicker' );
Then this is the input and jquery in my form (lower down on the plugin file):
<input type="text" class="date" name="ca_expiry_date" value="">
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.date').datepicker({
dateFormat : 'yy-mm-dd'
});
});
</script>
Can anyone help advise where I am going wrong with the datepicker?
Thank you!
UPDATE
Thanks for the comments, I have discovered that if I instal the "Classic Editor" plugin, my code works. But when that is not activated and using Gutenberg it does not work.
The error is (without sharing the site url):
GET https://xxxx.co.uk/wp-content/themes/publisher/gutenberg-7.6.2.min.css?ver=7.6.2 net::ERR_ABORTED 404 (Not Found)
This error is still there even when my plugin is disabled.
However, if I activate the Twenty Nineteen theme there is no error but the date field in my plugin still does not work.
So clearly gutenberg does not like something in my code...
I would make sure to register the whole jquery-ui package from the remote official source in the functions.php of your current theme, like this:
function ca_load_jquery_ui() {
// first, register the style from the remote official source
wp_register_style('jqueryuicss', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.min.css', array('jquery-ui-styles'), '1.12.1');
wp_enqueue_style('jqueryuicss');
// then, register the core file from the remote official source, in footer
wp_register_script('jqueryui', '//code.jquery.com/ui/1.12.1/jquery-ui.min.js', array('jquery-ui'), '1.12.1', true);
wp_enqueue_script('jqueryui');
}
add_action( 'wp_enqueue_scripts', 'ca_load_jquery_ui' );
Note: if you want to use your calendar in the frontend, please make sure to use the function add_action( 'wp_enqueue_scripts', 'xxx' ); instead of add_action( 'admin_enqueue_scripts', 'xxx' ); which is only for the admin side.
UPDATE
You might also try to disable the default jQuery or jQuery-ui to see if that helps:
//remove the default jQuery script
function remove_default_jquery(&$scripts){
if(is_admin()){
// try this...
$scripts->remove('jquery');
// or...
$scripts->remove('jquery-ui');
}
}
add_filter( 'wp_default_scripts', 'remove_default_jquery' );
I'm developing a wordpress site where the posts are loaded in a popup with ajax (with Magnific Popup). The posts are using the template single.php.
This works fine, except that the header and footer are also loaded in the popup (html tags, navigation, scripts, …). I can of course delete get_header() and get_footer() from the template, but then the single post pages aren't loaded correctly via permalinks.
I tried conditional tags, but when the template is loaded with Ajax, it doesn't 'see' it's loaded on the homepage.
I think working with different templates is an option, although I have seen sites that work with the same template (like the Zoo theme on themeforest). But I couldn't figure out how it works there.
So I'm stuck here. Anybody?
The clean solution would be to use the WordPress AJAX functions.
Themes are nowadays usually split in multiple parts to reuse blocks in various places. For example, the theme twentysixteen uses get_template_part( 'template-parts/content', 'single' ); in the single.php to include the template file that shows the actual content of the file. You can use this easily to get the content of your post without header, footer etc.
First, set up the PHP part, you can just add this in the functions.php of your theme or directly in your plugin, dependant on what you are developing.
<?php
// for the admin area
add_action( 'wp_ajax_my_action', 'my_action_callback' );
// for the public area
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
function my_action_callback() {
$postid = intval( $_POST['postid'] );
$post = get_post( $postid );
setup_postdata( $post );
get_template_part( 'template-parts/content', 'single' );
wp_die(); // this is required to terminate immediately and return a proper response
}
The corresponding JavaScript part:
var data = {
'action': 'my_action',
'postid': 1234
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
// on the frontend you have to set it yourself
var ajaxurl = '<?=admin_url( 'admin-ajax.php' )?>';
jQuery.post(ajaxurl, data, function(response) {
// alert('Got this from the server: ' + response);
// response will now contain your post, without header, footer, sidebars etc.
});
my_action is the identifyer for the whole process and has to be consistent between the two parts.
Documentation for WordPress AJAX support: https://codex.wordpress.org/AJAX_in_Plugins
I finally found out that this option is included in the Magnific Popup plugin: "To modify content after it’s loaded, or to select and show just specific element from loaded file, there is a parseAjax callback".
But I'll accept the above answer as I think this is an elegant way, instead of loading the whole page and only show the part needed.
I add shortcode in such way:
add_shortcode( 'NAME', array( $shortcode, 'add_shortcode' ) );
add_filter( 'widget_text', 'do_shortcode' );
This shortcode display html form with filters.
This shortcode I use(add) in widjet, but the result of this form I want to display on content page.
So I click submit the form and the result displays on content page.
How can I do this in best way?
I will be glad of any help.
This may be a bit difficult to do in a eloquent manner. The reason is that most WP templates generate the content before they generate the widgets - in the majority of templates the content is generated in files like content-page.php and the widgets are generated from files like sidebar.php.
Your best bet is to use some jQuery to inject HTML after the fact. For example, in the content you can generate an empty tag like this:
<div id="post-load"></div>
and then have your widget call something like this:
<script>
$( document ).ready(function() {
$("#post-load").html('your html goes here');
});
</script>
I know this does not answer your question more completely, but it's difficult to assume what your starting point is.
This question is based on an unanswered question in Wordpress Development which has not gotten a solid answer.
I have a wordpress website which lists hotels. the url for a single hotel looks like:
/hotels/the-marriot-hotel
I also have a custom taxonomy for Locations, which allows me to browse the hotels in various locations, which works fine, the urls are like:
/Locations/Liverpool
For URL's like /hotels/* I would like to use a custom template, which I have done already and works fine.
The Problem
I also want to be able to drilldown the Locations taxonomy creating a breadcrumb type URL and also use a different template for the hotel page.
For Example, if a user is browsing /Locations/Liverpool and clicks the Marriot Hotel I would like it to click through to /Locations/Liverpool/the-marriot-hotel instead of /hotels/the-marriot-hotel and also use a slightly different template, which can also load a different sidebar and recommend other hotels in the area specific to the location slug in the URL
So basically I want two routes to a single post and a different template used based on the route used.
How would I go about implementing this?
What have I tried?
I've tried adding a new page and using a rewrite rule to point to it to be the locations hotel page.
I've tried adding a slug on the end of the /Locations/{location-slug} url and reading this in the page template and loading the hotel post instead of the list it doesn't seem to be working but also feels like a terrible hack anyway
An idea that I've had is to add a rewrite to the hotels/{slug} page and using code to detect the URL used and switch templates dynamically but I'm not sure this is the best approach
I have managed to get this working using the second method mentioned above (adding a rewrite to the locations landing page and checking for a query_var).
I will post the code below that I used but although this works and seems to be working very well, It does not feel like the best way of doing it. If someone know of a better way of doing this please post the answer.
I used this online post for reference.
Note: The listing page shows the list of hotels in the taxonomy in a side column down the side and shows the currently selected or a random one in the main content area. Which will explain how I am using the loop below.
function prefix_locations_rewrite_rule() {
add_rewrite_rule( 'Locations/([^/]+)/([^/]+)', 'index.php?locations=$matches[1]&hotel=$matches[2]', 'top' );
}
function prefix_register_query_var( $vars ) {
$vars[] = 'hotel';
return $vars;
}
function prefix_url_rewrite_templates() {
if ( get_query_var( 'hotel' ) && is_singular( 'hotel' ) ) {
add_filter( 'template_include', function() {
return get_template_directory() . '/taxonomy-locations.php';
});
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
add_filter( 'query_vars', 'prefix_register_query_var' );
add_action( 'init', 'prefix_locations_rewrite_rule' );
In my template file for the hotels landing page:
$hotelSlug = get_query_var( 'hotel', false);
if ( have_posts() ) {
while (have_posts()) : the_post();
if ($post->post_name == $hotelSlug) {
break;
}
endwhile;
}
This bit of code will iterate over the posts and if the hotel slug matches the query var it will break there so that the current post is the one we wanted.
We could just use a query here but as I already have a list of posts within the taxonomy I thought I'd just iterate over it. Below this I check to see if a specific hotel has been selected otherwise I show a random one from the list.
I am still to add additional logic and error handling to this code, I hope it helps someone with a similar issue
By default ACF has two choices for the WYSIWYG custom field regarding toolbars, Full and Basic. These are both great but I just need one more button( maybe more in another project) in the Basic toolbar.
I am trying to add the text-color picker to the Basic toolbar.
Based on this documentation here, ACF Documentation, I came up with this:
add_filter( 'acf/fields/wysiwyg/toolbars' , 'my_toolbars' );
function my_toolbars( $toolbars ) {
array_unshift( $toolbars['Basic' ] , 'forecolor' );
return $toolbars;
}
I also looked at this past question,"How to add a button to ACF tiny MCE editor", but didn't find the links provided very clear in producing a result(maybe partially because comments were in French).
I am guessing it has something to do with what was said in that questions comments about missing the plugin for that button. But I am unsure, any solutions?
With some help from the Advanced Custom Fields Support Forum, I found out that my code was prepending to the outer array and I needed to target the nested array to add the button.
This is the code that works to add the Font Color button to the Basic Toolbar:
add_filter( 'acf/fields/wysiwyg/toolbars' , 'my_toolbars' );
function my_toolbars( $toolbars ) {
array_unshift( $toolbars['Basic' ][1], 'forecolor' );
return $toolbars;
}