Wordpress : Add a custom field directly above the title input field (w/o modifiying core files)? - wordpress

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:

Related

How to add search button in wordpress Menu

I want a search button at the right side of the primary menu which display a searchbox under primary menu once it is clicked.I am a newbie in wordpress and only know html and php basic.Can anyone please help me out?
To add the Custom Items into your menu , Then you should use the filter hook wp_nav_menu_items.
Please see the below Code and paste it into the current active theme functions.php file :
add_filter('wp_nav_menu_items','add_search_box_into_menu', 10, 2);
function add_search_box_into_menu( $nav, $args ) {
if( $args->theme_location == 'primary' )
$nav .= '<li class="custom-header-search"><form action="'.home_url( "/" ).' id="searchform" method="get"><input type="text" name="s" id="s" placeholder="Search"></form></li>';
return $nav;
}
For more help : See here
You'll need to add not just a link, but the search form.
To do so, you need to customize your theme.
Important: if you use a theme you didn't create, then first create a child theme and modify it (it will ensure you that your changes are still applied ethen if the theme recieve updated).
Then in the HTML/PHP code where you menu is, you can use the get_search_form() method to display the search form in place.
If you want to customize the search form, juste create a searchform.php file in your theme folder and customize it.
More informations here : https://codex.wordpress.org/Styling_Theme_Forms

Replacing only parts of an archive and single page template of WordPress

I am having a bit of trouble here understanding how to do the following. I have searched for weeks now but cannot seem to find what I am looking for.
I have a custom post type 'product' and want to change which template gets loaded for the single product page as well as the archive for the products. I am using the following code to load include and load templates.
add_filter('template_include', function() {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
if ('product' == get_post_type() ){
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
});
The problem I am having is that it replaces the current theme's template instead of just the inner part of the content and archive areas.
Here is what I want to achieve:
Create a custom post type 'product' in a plugin - DONE (Was kinda easy!)
When opening a single product only change the content part. - I can do this with the_content filter hook. Simple enough. Any other suggestions is welcome.
When I go to the archive view for the 'product' custom post type I don't want to have it load the theme's default archive (list) view but instead a grid view from my plugin which I cannot seem to get right. I only want to change the inner part of the template, not the whole page.
I have created this plugin a few weeks ago using only shortcodes which works good but want to see if I can do it without the use of shortcodes by means of creating the custom post type and changing the inner template parts of the current active theme.
Can anybody steer me into the right direction here?
If I create a theme I can do what I am looking for but I want to create this into a plugin instead without adding or making changes to the active theme. The plugin should handle what is needed.
The same issue is discussed here but what I want is to develop something that is theme independent. No changes should be made in theme files and no theme files should be copied to the plugin.
WP - Use file in plugin directory as custom Page Template?
Recently I also had the same problem. Here's how I worked it out.
template_include filter accepts a parameter which is the selected template that you want to override (this what you are missing in your code).
I don't know but sometimes the filter hook need higher priority to work like 9999. But first check if it work with default priority, if don't change it.
I assume your both archive and single product template both have include get_header() and get_footer() which can be used for default selected theme (Or if the theme has different setup, setup accordingly).
This is simplified code:
add_filter('template_include', function($default_template) {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
} else if ('product' == get_post_type() ) {
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
}
// Load new template also fallback if both condition fails load default
return $default_template;
}, 9999); // set priority, only if not worked with default one
The best option in this case is to provide a shortcode to the user. So they can place it on any page that they want (or that you auto generate). That way you will place your content inside their theme.
Something like this:
add_shortcode( 'slotsl-game', 'embed_game' );
/**
* Print the game
* #return false|string
*/
function embed_game(){
ob_start();
$game = get_post();
include_once SLOTSL_PLUGIN_DIR . 'templates/slotsl-single-game.php';
return ob_get_clean();
}

how to create multiple shortcodes in Wordpress and selectively position them anywhere in a custom template page file

In my functions file I have this:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
In the CMS page editor I have this:
[caption]My Caption[/caption]
This page is utilizing a custom template file template-mypage.php. My question is: I would like to create multiple short codes types within the CMS such as:
[caption1]My Caption[/caption1]
[caption2]My Caption[/caption2]
[caption3]My Caption[/caption3]
then in my template-mypage.php... I would like to selectively choose where to place [caption1], [caption2], [caption3]... for example [caption1] will go somewhere on the top... [caption2] in the middle and [caption3] towards the bottom of the template-mypage.php, all seperated by some huge chunks of HTML content. I do not want to write any HTML within the WP CMS... all HTML should be written in the template-mypage.php.
Currently I believe WP limits shortcode output to come out of the_content(); Is it possible to do something like the_content_of_caption1(), the_content_of_caption2(), the_content_of_caption3()?
Thanks please let me know!
this product does this perfectly
http://wordpress.org/plugins/multiple-content-blocks/

Content elements for wordpress

I'm looking for a plugin (or better yet, not a plugin) for wordpress that lets me generate standard content elements, or includes for posts and pages.
For example, my_content_1 could be:
buy it now for $23!!
Which could then be included in posts and pages using some kind of syntax (or whatever) like:
Welcome to my site, blah blah blah.. check out this product - %my_content_1%
Not looking for anything fancy, anything that does this sort of thing would be awesome.
The point of this being much like a regular php include I could have the same information updated in one place and applied over many pages/posts.
I found something that is pretty much what I'm looking for:
http://wordpress.org/extend/plugins/reusables/
However, other suggestions would be good as I'm not too confident in the quality of the code for that plugin.
Not sure about a plugin, but how about simply creating something yourself? If you created a PHP page and set up variables such as
$content->title = "This is a title"
$content->smallText = "Insert some short paragraph here"
And then just include it in your header? You could store it in your theme directory and then call it like so
<?php $themeFolder = get_bloginfo("template_url"); ?>
<?php include($themeFolder."/content.php") ?>
Would that be suitable?
How about creating a few files and link them in using shortcode?
ie: open your themes/functions.php file add this..
<?php
function wp_my_shortcodes($atts)
{
extract(shortcode_atts(array(
'type' => '', //author, rss, adverts
), $atts));
switch($type) {
case 'author' : $display = wp_display_author_info(); break;
case 'rssview' : $display = wp_display_rss_info(); break;
case 'adverts' : $display = wp_display_adverts(); break;
default : $display = wp_display_author_info(); break;
}
return $display ;
}
add_shortcode('mycontent', wp_my_shortcodes);
function wp_display_author_info()
{
include(TEMPLATEPATH.'/my_author_info.php');
}
function wp_display_rss_info()
{
include(TEMPLATEPATH.'/my_rss_info.php');
}
function wp_display_adverts()
{
include(TEMPLATEPATH.'/my_adverts.php');
}
?>
using shortcodes inside your posts you can then bring in which ever piece of content that you want.. in the example above I've created 3 pages in the template root folder called
my_author_info.php, my_rss_info.php, my_adverts.php all of which speak for themself..
my_author_info.php
this page could use the the_author_meta() to populate a div box with included author info,
my_rss_info.php
include your subscription box to let users subscribe to your blog
my_adverts.php
include 4x 125x125 adverts?
so in the post i could use
[mycontent type='author']
[mycontent type='rssview']
[mycontent type='adverts']
if no argument is added to the shortcode then the default view is shown, in this case..
[mycontent]
would return the authorview as default...
this would then include that file in the content...
just remember to create the included files :)
I found something that is pretty much what I'm looking for:
http://wordpress.org/extend/plugins/reusables/

Adding custom tags in Wordpress

I'm creating a new WP theme and I would like to allow the user to insert a divider in between paragraphs or images he/she is entering, for a post/page.
I want the output to be something like:
<div class="divider"></div>
But I don't want the user to have to enter HTML in the WYSIWYG editor. Is it possible to ask them to enter something like:
<-- break -->
and then translate that to the div markup on display?
Thanks.
Build a function in your theme's functions.php file like this:
function add_div( $content ) {
$content = str_replace( '<!-- break -->', '<div class="divider"></div>', $content );
return $content;
}
then add the following to the theme:
add_filter( "the_content", "add_div" );
The function uses PHP's string replace function to find the text you want your users to input and replace it with the text you want to render, the add_filter() function uses Wordpress's content filter to apply your function to the content of each post after it is read from the database, but before it is rendered to the browser.
This will work in PHP4 and up, which is still the official level of support for Wordpress.

Resources