Plugin Development: Dynamically change post title (the_title()) from a plugin, without changing database title - wordpress

I am developing a custom plugin that needs to change the post title dynamically (NOT the "title" tag for the head) without using a page template.
I want the end user of the plugin to be able to use their own theme templates, which will already output the title, usually in an h1 tag, but I don't know where and how for sure, because it is not my theme.
I will need to just change the output on the frontend, not in the database. I want to change it based on whether a url's query string matches certain values, or whether the frontend user is logged in or not. I know how to do this from a custom template, but I want to do this from a template the user already has selected.
I noticed that if I filter the_title(), it changes it everywhere in the admin dashboard and in the frontend navigation menus, which is what I don't want, I just want to change where it is outputted in the user's page template (usually in an h1 tag).... does anyone have any suggestions on how to go about this?

You can use the_title filter, which would have influence only on front-end, this doesn't change record in Database. Your code would be something like this:
add_filter( 'the_title', 'add_text_to_post_title' );
function add_text_to_page_title( $title ) {
if( is_post() )
$title = 'Your text: '. $title;
return $title;
}

Related

How would I structure this url functionality in wordpress

What I'm trying to do in Wordpress is import movie data from the movie database API, and display it inside a wordpress site. I'm just having trouble figuring out what would be the best way to set this up in the backend with a clean url structure.
I created a template file, and its going to have a GET variable with the movies title in it. I would want my domain to look like
mydomain.com/movie/TITLEGOESHERE
how could i set this up through wordpress, so the page I created works that way.
This would not be pulling post data from inside the wordpress. Would htaccess work here?
EDIT:
The way im thinking of it right now with a dirty url is, mydomain.com/pagewithAPIcode/?movie=MOVIETITLE, that way I can get the MOVIE TITLE as a GET variable, and then pass it to the api to grab the external movie info. And then with htaccess, rewrite it so it looks nicer
In the following code, a query variable mvtitle is added in which the movie title will be available. A rewrite rule is also created which for addresses matching the movie/{some-title} scheme displays the page movie (post type page). To read value of the mvtitle use get_query_var() function.
add_filter( 'query_vars', 'so59386348_query_vars' );
add_filter( 'page_rewrite_rules', 'so59386348_page_rewrites' );
function so59386348_query_vars( $vars )
{
$vars[] = "mvtitle";
return $vars;
}
function so59386348_page_rewrites( $rewrite_rules )
{
$page_slug = 'movie';
$rewrite_rules["$page_slug/([^/]+){1}/?$"] = 'index.php?pagename='.$page_slug.'&post_type=page&mvtitle=$matches[1]';
return $rewrite_rules;
}
$title = get_query_var( 'mvtitle', FALSE );
echo 'Movie title: ' . $title;
Please follow below instructions:
Log in to your WordPress website.
When you're logged in, you will be in your 'Dashboard'.
Click on 'Settings'.
On the left-hand side, you will see a menu. In that menu, click on 'Settings'.
Click on 'Permalinks'.
The 'Settings' menu will expand providing you additional options. Click on 'Permalinks'.
Select 'Post name'.
Click 'Save changes'.
I hope it would help you out.
Open dashboard
Search for settings
Click to permalinks in settings
Look for Permalinks common settings
in Custom structure use the url like %category%/%postname% and save
Enjoy!!
First of all open the admin panel, Go to settings->permalinks and in the custom settings, change the custom structure such as:
%custom_name%/%postname%
You can change the custom name with category or any other static name you want.

Wordpress template for specific nested page

In Wordpress I have a page with the slug logged-user with a parent page ecadmin, making it accessible under the URL /ecadmin/logged-user/.
Since it has a parent page, I (or any other user) can create a page with the same slug logged-user as long as it isn't nested under the same parent page.
The problem now is that I cannot create a single page template page-logged-user.php in theme's folder, as this template can be potentially applied to any other page named logged user no matter where it belongs hierarchly.
Is there a way to name the template file in such way that is referencing to its parent page(s) as well? E.g. page-ecadmin-logged-user.php
The feature you are hoping for doesn't exist, but there is a quite simple way to accomplish what you want to do:
You can create a custom page template [whatever-you-want].php and place this comment in the header
/*
Template Name: Logged In User
*/
That template will be available in the Page Attributes meta box for any "Page" in Wordpress, but will only be applied to a given page if selected in that meta box.
Note:
All content creators have access to the page attributes meta box by default.
If you are worried about users applying that template inappropriately you could hide the Page Attributes meta box from everyone who isn't an admin:
function remove_post_custom_fields() {
if (!current_user_can( 'create_users' )){
remove_meta_box( 'pageparentdiv' , 'post' , 'normal' );
}
}
add_action( 'admin_menu' , 'remove_post_custom_fields' );
http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/
You could try using using page-$id.php which would be unique to that page.

How to find the current theme in any page of Drupal 7

As the title says,
I need to know the name of the theme being used in any drupal page.
For example we can set a default theme and a admin theme for our drupal site basically. For now lets consider only this part and don't get into user specific themes.
So in the above case the home page will load using default_theme and when you do to any admin pages, they will load in admin_theme(seven/bartik).
Now my question is I need to know what theme is being loaded when I navigate to any page of the site. May it be a node/add or node/edit or admin/* or page/* or any page.
Simply when I go to any page I shoudl know the theme name being used in that page and in which hook I can write my conditions.
Based on that I want to do some changes.
I tried using globals $theme, path_to_theme, drupal_get_path etc
When I use them ,they always return the default theme name and not the theme being used in a specific page.
I wrote exit statement for $variables in hook_preprocess_html and it returns value by exiting only if the page is normal users page. But not in admin pages like sitename/admin/content etc.,
Any one who can help.
Thanks in advance
Create a custom module, and put your code in function hook_init(){} in your .module.
No arguments.
function example_init() {
global $theme;
print $theme.'<br />'; // current theme name
print $GLOBALS['theme'].'<br />'; // current theme name
$theme_path = drupal_get_path('theme', $GLOBALS['theme']);
print $theme_path.'<br />'; // path to current theme
}

Dropdown of existing posts in a metabox

I want to have ability to choose for each page what post should appear in a sidebar, from multiple posts type. So I understand that I need a meta box with a dropdown list of all posts, but I don't know how to build this in functions.
I only found this solution which is quite similar to what I want, but this doesn't help me to much, because I can only choose from a single post type and display only in post pages.
There is a free plugin that will solve all of your woes. It's called ACF or Advanced Custom Fields. It has the ability to add a list of posts to a field and attach that field to pages. Here's how you'd do it:
First install the plugin and navigate to the custom fields screen. Setup your field exactly like this:
Then in the options below that section you need to select these options:
That will tell ACF to put the field only on pages. After you have set that up you will get a little sidebar block like this:
You can then select each post for the page and it will return that object on the frontend. You do need to use a little code to get the frontend to spit out the posts you need. Here is the code to get a frontend option from ACF. Inside of the sidebar.php file you need to add this code:
global $post; // Get the global post object
$sidebar_posts = get_field('posts', $post->ID); // Get the field using the post ID
foreach($sidebar_posts as $sidebar_post){ // Loop through posts
echo $sidebar_post->post_title; // Echo the post title
}
This will simply loop through the posts you select and echo out the title. You can do more with this by adding some other Wordpress post functions using setup_postdata(). This will allow you to do things like the_title() and the_content().
Hope this helps!

How to disable page's title in wp-admin from being edited?

I have a wp-network installed with users that can create pages in each site.
Each of those pages get a place in the primary menu, and only one user have permission to create all this menu.
I want to create a user only to be able to edit the content of the pages, but not the title.
How can I disable the title of the page to be edited from the admin menu for a specific user, or (far better) for a capability?
I thought only a possibility, that's editing admin css to hide the title textbox, but I have two problems:
I don't like to css-hide things.
I don't know where is the admin css.
I know php, but don't know how to add a css hide to an element for a capability.
You should definitely use CSS to hide the div#titlediv. You'll want the title to show in the markup so the form submission, validation, etc continues to operate smoothly.
Some elements you'll need to know to implement this solution:
current_user_can() is a boolean function that tests if the current logged in user has a capability or role.
You can add style in line via the admin_head action, or using wp_enqueue_style if you'd like to store it in a separate CSS file.
Here is a code snippet that will do the job, place it where you find fit, functions.php in your theme works. I'd put it inside a network activated plugin if you're using different themes in your network:
<?php
add_action('admin_head', 'maybe_modify_admin_css');
function maybe_modify_admin_css() {
if (current_user_can('specific_capability')) {
?>
<style>
div#titlediv {
display: none;
}
</style>
<?php
}
}
?>
I resolved the problem, just if someone comes here using a search engine, I post the solution.
Doing some research, I found the part of the code where the title textbox gets inserted, and I found a function to know if a user has a certain capability.
The file where the title textbox gets added is /wp-admin/edit-form-advanced.php. This is the line before the textbox
if ( post_type_supports($post_type, 'title') )
I changed it to this
if ( post_type_supports($post_type, 'title') and current_user_can('edit_title') )
That way, the textbox is only added when the user has the capability called "edit_title"
When this IF block ends few lines after, I added:
else echo "<h2>".esc_attr( htmlspecialchars( $post->post_title ) )."</h2>";
To see the page title but not to edit it, when the user hasn't got "edit_title" capability.
Then I had already installed a plugin to edit user capabilities and roles, wich help me to create a new capability (edit_title) and assign it to the role I want.

Resources