Wordpress - How to set title dynamically based on content? - wordpress

I try to set custom titles using data from the content. Those are not pages nor posts - plugin (chaturbate) that I'm fixing somehow process urls like /lala-fafa without having any entries in the posts table.
Here is my code
add_filter( 'the_title', 'chfix_alter_title', 9999);
function chfix_alter_title($title, $id=null)
{
global $chfix_values;
return "$chfix_values[Name] $chfix_values[Age]...";
}
add_filter( 'the_content', 'chfix_doit', 9999 );
function chfix_doit($content)
{
global $chfix_names, $chfix_values;
if (!is_admin())
{
foreach($chfix_names as $name)
{
// parsing content
$chfix_values = chfix_get_field($content, $name);
}
}
return $content;
}
The problem is that the_title filter is called AFTER the_content filter. I also tried wpseo_title, widget_title with no luck. Is any possibility to make what I want?

Related

How to get post id using add_my_endpoint?

I'm creating AMP pages on my own, without a plugin, and I have one problem that I can't solve.
function h34_endpoints_add_endpoint_pinup()
{
add_rewrite_endpoint('amp', EP_ALL);
}
add_action('init', 'h34_endpoints_add_endpoint_pinup');
add_filter('template_include', 'amp_page_template_pinup', 2);
function amp_page_template_pinup($template)
{
if (get_query_var('amp', false) !== false) {
$template = plugin_dir_path(__FILE__) . 'amp-template.php';
}
return $template;
}
Now in the amp-template.php file ш need to get the post data (if it is a post) but where and how to get the post ID?
global $post; shows nothing
get_the_ID() - doesn't output anything either.
I will be grateful for any help

Can I add content before post content without using the_content filter

I am adding filter to add embed before post content, but don't want add to other places where called apply_filters("the_content").
So i need add embed before(after) post content without "the_content" filter hook.
add_filter('the_content', 'embed_on_post');
if you want to filter only a certain type or such, you can add your verification inside your filter function:
source: wordpress documentation
add_filter( 'the_content', 'my_function' );
function my_function( $content ) {
//for a single post
if (is_singular('post')) {
/* do whant you want */
}
//or also (works for posts except attachments and pages)
if (is_single()){
}
return $content;
}

Remove the Product Count on Woocommerce Categories

pic http://goo.gl/hnRfG9
I want to hide product count form category : I tried this in functions.php of theme
add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
function woo_remove_category_products_count() {
return;
}
But it does not work. I edit this from theme editor and also from directory to make sure its written.
That one works for me:
add_filter( 'woocommerce_subcategory_count_html', 'jk_hide_category_count' );
function jk_hide_category_count() {
// No count
}
Picked it up in WooC tuts somewhere.
Try this one:
/** Remove Showing results functionality site-wide */
function woocommerce_result_count() {
return;
}

How to show text in a page on Wordpress whithin a plugin

I am developing a plugin for a website.(Which is my first plugin for Wordpress)
The basic functionality is querying the database and in specific pages show the data from the database with a specific style instead of the content from the pages.
So far I managed to show some text in every specific page.
This is my code after some basic configurations:
global $wpdb;
global $wp_query;
add_action( 'wp', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I've read this post, but I couldn't solve my problem.
WordPress replace content of a page (plugin)
I already read the Wordpress documentation but I havent find anything there.
I found myself a solution, using shortcodes.
global $wpdb;
global $wp_query;
add_shortcode( 'sector_page_display', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function medicalPage()
{
return print "Medical";
}
See that instead of add_action I changed to add_shortcode
Then on everypage I will use to show info from the database I add
[sector_page_display]
in the page, so it call my method. You can add variables in there if you want.
You'll want to run that code before WordPress has fully loaded.
Try this
global $wpdb;
global $wp_query;
add_action( 'init', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I changed the add_action to now run the code when WordPress is being initialized.

Wordpress - Apply remove_filter only on one page

I am inserting multiple pages in one page (with showmultiplepages plugin), and one page includes a php file (with exec-php).
I want to disable a filter only for this included page. If I add
remove_filter( 'the_content', 'wpautop' );
to my included page, any page comming after this page won't have filters too.
Is there some tag like 'the_page' so that only the page will have no filter?
Thanks for help.
I know this is an old question, but I thought I'd chime in for anyone looking to do this in a more general sense (e.g. not in conjunction with plugin output) and say that you could also just add this to your functions.php file:
add_filter('the_content', 'specific_no_wpautop', 9);
function specific_no_wpautop($content) {
if (is_page('YOUR PAGE')) { // or whatever other condition you like
remove_filter( 'the_content', 'wpautop' );
return $content;
} else {
return $content;
}
}
I suggest creating a page template for the "one page includes a php file (with exec-php)". Then add an if statement around the remove_filter(...) statement.
if (!is_page_template('my-page.php'))
remove_filter('the_content', 'wpautop');
Hope it works. ;P
Like mroncetwice, I too realize that this is an old question; however, I came to this thread looking for an answer when I saw his. I decided to improve upon it (in terms of suiting my own situation) and am sharing the results with the hope that it may also help others.
Turn wpautop on or off by default and list any exceptions:
/**
* Allow or remove wpautop based on criteria
*/
function conditional_wpautop($content) {
// true = wpautop is ON unless any exceptions are met
// false = wpautop is OFF unless any exceptions are met
$wpautop_on_by_default = true;
// List exceptions here (each exception should either return true or false)
$exceptions = array(
is_page_template('page-example-template.php'),
is_page('example-page'),
);
// Checks to see if any exceptions are met // Returns true or false
$exception_is_met = in_array(true, $exceptions);
// Returns the content
if ($wpautop_on_by_default==$exception_is_met) {
remove_filter('the_content','wpautop');
return $content;
} else {
return $content;
}
}
add_filter('the_content', 'conditional_wpautop', 9);
Does not work?
add_filter('the_content', 'specific_no_wpautop', 9);
function specific_no_wpautop($content) {
global $post;
if (is_single('5136') || ('3820')){ // or whatever other condition you like
remove_filter( 'the_content', 'wpautop' );
return $content;
} else {
return $content;
}
}

Resources