0

I'll start by saying I'm fairly new to coding so I'm probably going about this the wrong way.

Basically I've got the below php function that changes urls to the page title of the url instead of a plain web address. So instead of www.google.com it would appear as Google.

<?php
function get_title($url){
  $str = file_get_contents($url);
  if(strlen($str)>0){
    $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
    preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
    return $title[1];
  }
} 
?>

This is great but to implement this I have to use the below code.

echo get_title("http://www.google.com/");

However this just works on a predefined URL. What I have set up on my site at the moment is a shortcode in a html widget.

<a href='[rwmb_meta meta_key="link_1"]'>[rwmb_meta meta_key="link_1"]</a>

This shortcode displays a url/link that is input by the user in the backend of Wordpress and displays it on the frontend as a link. However I want to apply the get_title function to the above shortcode so instead of the web address it shows the page title.

Is this possible?

Thanks in advance.

1 Answer 1

0

for name of a url from a link you can use parse_url($url, PHP_URL_HOST);


easier way would be to have an array of links for example

$links[] = 'some1 url here';
$links[] = 'some2 url here';

then just loop your $links array with the function.

foreach($links as $link)get_title($link);

https://metabox.io/docs/get-meta-value/

try:

$files = rwmb_meta( 'info' ); // Since 4.8.0
$files = rwmb_meta( 'info', 'type=file' ); // Prior to 4.8.0

if ( !empty( $files ) ) {
    foreach ( $files as $file ) {
        echo $file['url'];
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your reply. I added your code to the functions.php file. However it hasnt affected the shortcode, what else do I need to do to get this to work?
oh its just an example from the docs to show that you can take links from the rwmb_meta, in your example it is probably not $file['url'], but $file['link_1']
its still unclear for me what you are trying to do, where do you get your links from and why would you want to change titles..
I'm using a wordpress plugin called metabox, below is a few screenshots of the backend and the frontend which might give you a better idea. imgur.com/a/GQGhx
from the metabox.io/docs/get-meta-value, echo $url = get_post_meta( get_the_ID(), 'field_id', true );
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.