Wordpress permalink from the database? - asp.net

I am trying to access selected post information from my WordPress database to display on a second website which is not a WordPress site nor is it php, it is in fact an asp.net website that i want to display the information on.
I have a WordPress blog at blog.domain.com and another website at domain.com both running on different servers and I need to display the excerpt, title with link on my asp.net website for selected posts. I can easily grab the excerpt and title from the blog's database but the only problem is that the permalink is not in the database.
I had the idea that maybe if I place a short piece of code on every blog to insert the permalink to a new table, i could then grab that to use over on the other website. But that didnt work, perhaps I am not doing it right. This is the code I used;
<?php
global $wpdb;
$table_name = $wpdb->prefix . "posts_pages_url";
$wpdb->insert($table_name , array('post_id' => the_ID(), 'url' => the_permalink()));
?>
Some might say, "why dont you just use an RSS Feed?" Well if it comes down to it, I just might have to, but i would prefer not to as connecting to the database directly to access my selected posts is much more flexible.
I would be very grateful if anyone had any suggestions on how I can access the permalink via a database call.
Many thanks

You could use get_permalink() in your code, or link to the guid value in the DB - not ideal I know, but it should at least work.
EDIT:
Same goes for the_ID() - it should be changed to get_the_ID().

This code will get it for you if using Yoast
$q = "SELECT * FROM wp_yoast_indexable yi
WHERE object_type = 'post'
AND object_id = '$ref'";
$yis = $scDb->select($q);
$permaLink = "";
if(isset($yis[0]))
{
$permaLink = $yis[0]->permalink;
}

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 add rewrite rule doesn't work for me

I'm developing a plugin that creates 2 pages: one listing page and one details page.
I don't use custom types. I have created a table into the database where I save the company details and also a slug for the details page.
It's working properly but I have a problem with the URLs. They are not pretty.
On the listing page I use this code to create the link:
<?php echo stripslashes($value->company_name); ?>
The generated link looks like this:
https://www.website.com/company/details/?companyname=new-company-name
I use the query because I need it on the details page, where I use this code:
$company_slug = $_GET['companyname'];
$company_details = $wpdb->get_row("SELECT * FROM $table_company WHERE company_slug = '$company_slug'");
This is how I retrieve the company details from sql and it also works just fine.
I have created manually in Wordpress the details page.
The problem is that I want the details URL to look pretty, like this:
https://www.website.com/company/details/new-company-name/
Generating it like this it's easy but when I click, I get a 404, since the query from URL is missing.
I was thinking it's easy to create directly the pretty URL and on the details page to parse the URL and get the company slug. It didn't work. I get a 404 maybe because the page doesn't physically exist.
So, I've done some research about URL rewrite and I have found some examples but none worked.
I have found tried this code also:
add_filter('query_vars', function($vars) {
$vars[] = "companyname";
return $vars;
});
function custom_rewrite_rule() {
add_rewrite_rule('^companyname/?([^/]*)/?','company/details/?companyname=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
I've read that I shouldn't use matches if I use a custom URL instead of index.php, so I have also tried without matches:
add_rewrite_rule('^companyname/?([^/]*)/?','company/details/?companyname=$1','top');
No results. If course, after every change I have saved again the permalinks.
This should be an easy task but somehow it doesn't work.
https://developer.wordpress.org/reference/functions/add_rewrite_rule/
Does anyone of you know how can I make this work?
Thank you.
Regards,
AG
I am assuming that the details page you created has the slug company/details. Here's what you need to do to make it work-
1. Add the custom rewrite rules in functions.php file:
function theme_custom_rewrites() {
add_rewrite_tag("%companyname%", "([a-z0-9\-_]+)");
add_rewrite_rule('^company/details/([a-z0-9\-_]+)/?$', 'index.php?pagename=company/details&companyname=$matches[1]', 'top');
}
add_action('init', 'theme_custom_rewrites');
It registers a new rewrite tag/query var named companyname to be used later and registers a custom rewrite rule for the specific URL structure you want (/company/details/company_name).
2. Get the company name on the template file and use it: After you have added the above code and saved the permalinks, you can get the companyname just by using the get_query_var() function.
$companyname = get_query_var( 'companyname' );
Hope it helps. Thanks.
Thank you very much for your fast reply. It worked.
Of course, I had to change the link on the listing page to:
<?php echo stripslashes($value->company_name); ?>
I have removed the query from the link and it looks like this now:
https://www.website.com/company/details/new-company-name/
What I don't understand, is how does WP know which is the query, since I removed it from the link.
I can see the same data if I access
https://www.website.com/company/details/?companyname=new-company-name
or
https://www.website.com/company/details/new-company-name/
But, basically, this part (?companyname=) doesn't exist anymore in the link, since I changed it.
I have no query now in my plugin, but somehow everything works properly. :)
I did not declare it somewhere. It's completely gone and it works.
How does this code know that the query exists and it's retrieving the slug from the database?
$companyname = get_query_var( 'companyname' );
I only have this code now:
<?php echo stripslashes($value->company_name); ?>
So, no query in URL.
Thank you for your time.
Regards,
AG

Seo yoast plugin, breadcrumbs

This is question on the seo yoast wordporess plugin.
I have a friend who doesn't know that much about code/wordpress/etc, they have an ecommerce site built using magento and a blog using wordpress which is styled to match the main site and they use yoast seo plugin for seo etc. They have asked me to try an get the breadcrumbs working for them in a different way to what i know, from what i can see they want to add a prefix to the breadcrumbs 'home' which links to main site then renaming the blog from home to blog, for example home(main site) > blog(blog) > post.
Now i did just that in the plugin settings but they said that the schema markup wouldn't be complete and said there’s a filter hook called ‘wpseo_breadcrumb_links’ which gives access to the array of URLs and anchor text used to build the breadcrumbs, now i cant find anything on google that explains this or how to start writing it, i do know that it needs to go in the functions file.
Would it be possible to get some help on this.
Thanks in advance and very much appreciated.
Justin
I'm not sure what Schema.org markup has to do with breadcrumbs! :) Schema is used on post/page level. So, they are either oversmarting themselves or I got you wrong.
I think this sample code may be helpful:
add_filter( 'wpseo_breadcrumb_output', 'custom_wpseo_breadcrumb_output' );
function custom_wpseo_breadcrumb_output( $output ){
if( is_product() ){
$from = '<span typeof="v:Breadcrumb">Products</span> »';
$to = '';
$output = str_replace( $from, $to, $output );
}
return $output;
}
It looks like wpseo_breadcrumb_output gives you access to the entire output instead of just the link portion. Please see this page for more details: http://wpquestions.com/question/showChrono/id/8603

Templates and pulling data from my own database (not wordpress one)

I'm pretty new to wordpress. I have read a lot about templates, template pages, etc... and I would really apreciate if someone could confirm my thoughts, ideas about how to build my Homepage with wordpress (I won't be using it for blooging, but my enterprise webpage).
The main problem is that I need to have a page, where a little list of items will be shown. After one of them is clicked another page will be shown with information about the item like a pic, short description, etc... etc... This Item information page will be using a template page where data from my own CMS database has to be inserted in the template holes.
All examples I have seen in wordpress are about pushing data in wordpress database and showing it using WP API. But, what about if I just want to use wordpress as a template system, with all its plugins, etc... and pull the data needed to be shown from another database (our CMS)?. Could anyone suggest best practices for this, or a better aproach?.
Thanks in advance.
is your wordpress database in the same database as your own?
ie: you can access the wp tables along with all your other tables?
if so you can use the wp api, to connect,
using the normal connection methods..
if the tables are not in the same database but located under the same account on your host, you could use the same method shown in this thread...
$mydb = new wpdb('username','password','database','localhost');
$rows = $mydb->get_results("select Name from my_table");
echo "<ul>";
foreach ($rows as $obj) :
echo "<li>".$obj->Name."</li>";
endforeach;
echo "</ul>";
if the database is in another location, outwith the current server just change the localhost to your server ip and port to connect, ie:
$mydb = new wpdb('username','password','database','192.168.1.1:6807');
good luck
Marty

How can I display posts from the other sites in a WordPress multisite setup?

I have a small network of sites setup with WordPress 3.0's multisite feature. I would like to create another site which pulls certain posts from the various other sites to display. This new 'hub' site would seem like its own separate site to the user (with domain mapping), but its content is coming from the posts from the other sites.
How can I get posts from another site in a WordPress multisite setup? Can I query for posts based on the name of the site? The end result needs to be a collection of posts from the different sites sorted by date.
Thanks for your help.
I had the similar issue where I wanted to get posts from one blog and display it on an other I came up with the following solution which you could modify slightly to meet your needs if needed
<?php
global $switched;
switch_to_blog(2); //switched to 2
// Get latest Post
$latest_posts = get_posts('category=-3&numberposts=6&orderby=post_name&order=DSC');
$cnt =0;?>
<ul>
<?php foreach($latest_posts as $post) : setup_postdata($post);?>
<li>
<?php echo short_title('...', 7); ?>
</li>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site ?>
I'm also limiting the amount of words which is being out putted if you do not want this feature simple use
$post->post_title;
Hope it helps.
This wouldn't be terribly difficult to set up with direct database calls. You can query posts from any site on the install with the $wpdb object. See Displaying Posts Using a Custom Select Query for information on using a custom database query, but keep in mind that instead of selecting from $wpdb->posts you're going to need to access the specific site table you want posts from. On a default Wordpress 3 install, this would be wp_12_posts where 12 is the site id. The id can be found in the wp_blogs table, or by looking at the ID column in the Sites section of the admin menu.

Resources