I am creating a small custom test administrator page in WordPress and when I insert a wp_editor command the textarea was displayed on the top of the working area?
Unfortunately no CSS was able to restore it to its correct position. Any help or ideas will be greatly appreciated.
Edit: Thank you for your quick reply I am writing the very simple code below. As you can see it is nothing super, just a test:
function TstPlg_PageDisplay() {
$MyPage = '
<div class="wrap">
<h2>TestPlugin</h2>
<p>This is a test for the TestPlugin Page</p>
<hr>
<div>
'.wp_editor('','MyTextArea').'
</div>
</div>';
echo $MyPage;
}
function TstPlg_AddMenuPAge() {
add_menu_page( 'TstPlg_PageDisplay', 'Test Plugin', 'manage_options', 'TstPlg_PageDisplay', 'TstPlg_PageDisplay');
}
add_action ('admin_menu', 'TstPlg_AddMenuPAge');
It seems (it's hard without code), that the textarea is echoed and the plugin code is outputed directly.
To avoid this you can use ob_start(), ob_get_clean() and after echo the result (textarea) where you want.
Related
Our Wordpress site suddenly stopped showing all 'featured' images. They're just blank. When we look at the source code it shows url(896). We have tried selecting the image again (which shows fine in the backend) but on the frontend it's still blank with an invalid URL. Any idea how to fix that and what may have happened? TIA!
To update: this below code is what's being used to pull the featured images (with the incorrect URL):
<div class="hero homepage relative-block" style="background-image:url(<?php the_field('hero_background_image'); ?>);<?php $hero_aspect_ratio = get_field('hero_aspect_ratio'); if( $hero_aspect_ratio ) { echo "padding-top:".$hero_aspect_ratio."%;"; } ?>"></div>
EDIT:-
The following also isn't grabbing any content. Any ideas?
<div class="page-content">
<?php get_template_part('content-block-loop'); ?>
</div>
Your field hero_background_image returns attachment ID instead of the URL of the image. Use wp_get_attachment_url($id) with get_field() that returns the id, instead of the_field() that outputs the value to get the URL of the attachment/image:
<div class="hero homepage relative-block" style="background-image:url(<?php echo wp_get_attachment_url(get_field('hero_background_image')); ?>);<?php $hero_aspect_ratio = get_field('hero_aspect_ratio'); if( $hero_aspect_ratio ) { echo "padding-top:".$hero_aspect_ratio."%;"; } ?>"></div>
Possible cause: if you use some metabox/custom fields plugin as MetaBox, Toolset Types or the ACF, someone has probably changed the settings for the field, to return the ID of the attachment instead of the URL. So that might also solve the issue.
I am working on a plugin that will be used to add a customized form of Acuity Scheduling for a specific page. I want to add the scheduling form after the menu and page title on one particular page. Here is my current code:
add_action( 'template_redirect', 'check_if_acuity_page');
function check_if_acuity_page(){
if(is_page('Schedule Page')){
add_action( 'add to acuity', 'display_acuity_scheduling_api');
}
}
function display_acuity_scheduling_api(){
echo '<div style="margin-top: 25px;">"Code to add Acuity Schedule to page"</div>';
}
The 'add to acuity' is a custom action hook that is currently added in the header.php file of the theme I am using. It adds the schedule at the very top of the page currently, so I can at least get it on the proper page, but it is located above the Menu and Title for the page. I am working on creating a custom layout and using PHP code to modify the page depending on what the user chooses, which is why I am not just using a simple embed code.
I am new to Wordpress Plugins and Hooks so I am not sure if I am supposed to be using an action or filter hook for this. Any help would be very appreciated.
To add code just before content which is below page title use following code:
function check_if_acuity_page(){
if(is_page('Schedule Page')){
echo '<div style="margin-top: 25px;">"Code to add Acuity Schedule to page"</div>';}
}
function add_code_before_content($content){
$acuity_page = check_if_acuity_page();
$content = $acuity_page.$content;
return $content;
}
add_filter('the_content','add_code_before_content');
Hope this helps.
WordPress action hooks are a means of providing a way for other developers to insert their own code in specific locations within your code, in order to change or expand the functionality of your code.
So in this case you should be using an action hook.
The concept of filters and hooks is explained in this article.
So by placing the add_action function in your template after the menu and page title you can hook onto it with a function.
In your page template after the menu and page title:
add_action( 'add to acuity', 'check_if_acuity_page');
In your functions.php:
function check_if_acuity_page() {
if(is_page('Schedule Page')) {
echo '<div style="margin-top: 25px;">"Code to add Acuity Schedule to page"</div>';
}
}
Should be pretty straight forward but placing my filter in the themes function file is not having any affect on the template:
add_filter('term_description', 'filter_ptags_on_images');
function filter_ptags_on_images($content){
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
and my markup:
<?php echo term_description(); ?>
EDIT: I tried testing the filter by returning dummy content from the function and nothing changes, so the filter isn't running on the content for some reason
It turns out I was trying to target the wrong template and the actual function was an Advanced Custom Field wysiwg custom content tag:
<?php the_field('fieldname'); ?>
It took me a little while to figure out how to filter this but with a little googling I found out you can use this to target an ACF wysiwyg field from your functions file:
add_filter('acf_the_content', 'your_function');
I'm looking for a WP function that add the Read-only parameter to all Pages's Titles's input, that will make the Page's title unalterable.
Thanks a lot in advance.
This can be accomplished with some simple JavaScript/jQuery. Create a file called admin_title_disable.js, and queue it up within functions.php. For example:
functions.php:
wp_register_script('admin_title_disable', '/path/to/admin_title_disable.js');
function disableAdminTitle () {
wp_enqueue_script('admin_title_disable');
}
add_action('admin_enqueue_scripts', 'disableAdminTitle');
Now, in your js file:
jQuery(document).ready(function ($) {
$('#title').attr('disabled','disabled');
});
This will set both post and page title input fields with a disabled attribute. Hope this helps!
If you want to restrict this script to a particular admin page, wrap the add_action hook in a conditional that compares $_GET['page']. You can also take advantage of the $hook parameter that is available when using admin_enqueue_scripts to check for the page. See here.
Update::
WordPress makes it a little tricky to tell between post and page edit screens, but there is a hidden input that you can take advantage of. :) Here's an updated version of the jQuery that will only run on page edit screens:
jQuery(document).ready(function ($) {
//find the hidden post type input, and grab the value
if($('#post_type').val() === 'page'){
$('#title').attr('disabled','disabled');
}
});
No need to make a seperate js file. Adding this to your function.php will do the same that Matthew showed.
function admin_footer_hook(){
?>
<script type="text/javascript">
if(jQuery('#post_type').val() === 'post'){
jQuery('#title').prop('disabled', true);
}
</script>
<?php
}
add_action( 'admin_footer-post.php', 'admin_footer_hook' );
This Solution Will disable clicking on the post title and editing it using CSS. CSS targets post type "page" only. It has been tested on Gutenberg visual editor. Users Can still edit title from "Quick Edit".
Add this code to your functions.php file.
function disable_title_edit() {
if(!current_user_can('administrator')){
if( !current_user_can('administrator')){ ////Only allow Admin
echo '<style>.post-type-page .edit-post-visual-editor__post-title-wrapper{
pointer-events: none;
}</style>'; } }
}
add_action('admin_head', 'disable_title_edit', 100);
Does anyone know of a way to add an input field (or any type of html for the matter) directly above (or below) the title input field on the post edit page ?
I'm looking of a way to do this without modifying core files (I'm doing this as part of a plug-in which creates a custom post-type).
I'm not aware of any available wp hooks in that area of the edit-form-advanced.php file which could help out. I really hope some has come up with a genius workaround !
Since version 3.5 wordpress introduced new hooks for the add/edit post screen called edit_form_after_title and edit_form_after_editor. So now i think we can easily add new html element after wordpress input title and input content.
just use filter like this on your functions.php
add_action( 'edit_form_after_title', 'my_new_elem_after_title' );
function my_new_elem_after_title() {
echo '<h2>Your new element after title</h2>';
}
add_action( 'edit_form_after_editor', 'my_new_elem_after_editor' );
function my_new_elem_after_editor() {
echo '<h2>Your new element after content</h2>';
}
You're on the right track; pursue the add_action('admin_head') point of entry. What you want can specifically be done with a bit of JavaScript + jQuery (which is built into WP). To display the input field above the title input field, do something like this:
add_action('admin_head', 'my_admin_head_in_posts');
function my_admin_head_in_posts() {
?>
jQuery('#post').before(
'<div id="id_my_field" class="updated below-h2">' +
'<input type="text" name="my_field" value="lol" />' +
'</div>'
);
<?php
}
And you should be seeing something like this: