Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- для каждого сайта надо передить из wp-config.php этого же сайта
- dbname=название базы данных
- $user = имя пользователя;
- $pass = пароль;
- $table_prefix = префикс таблицы;
- копируем данный скрипт в папку public_html сайта
- входим по ssh
- в терминале вводим:
- cd папка_сайта/public_html/
- далее вводим команду
- php -f title_alt.php
- */
- $dsn = "mysql:host=127.0.0.1;dbname=krovldb;charset=utf8";
- $user = 'xxxx';
- $pass = 'xxxx';
- $table_prefix = 'xxx';
- $db = new PDO($dsn, $user, $pass);
- // setup PDO to throw an exception if an invalid query is provided
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $id = 0;
- $sql = "SELECT * FROM `{$table_prefix}posts` as `posts`
- WHERE `posts`.`ID`> {$id}
- AND `posts`.`post_content` LIKE '%[caption%]%title=%src=%[/caption]%'
- AND `posts`.`post_type` = 'post'
- ORDER BY `posts`.`ID` LIMIT 50";
- // Next, let's prepare a statement for execution, with a single placeholder
- $statement = $db->prepare($sql);
- // Create some parameters to fill the placeholders, and execute the statement
- $parameters = [ "221B" ];
- $statement->execute($parameters);
- $rows = $statement->fetchAll(PDO::FETCH_OBJ);
- while ( count($rows) > 0 ) {
- foreach ($rows as $row) {
- $content = $row->post_content;
- $content_arr = explode( '[/caption]', $content );
- $new_content_arr = array();
- foreach ($content_arr as $content_element) {
- $id = $row->ID;
- //Находим url
- $captionpos = mb_strpos( $content_element, '[caption id="attachment', 0,'UTF-8' );
- $href_pos_start = mb_strpos( $content_element, '<a href="', $captionpos,'UTF-8' );
- $href_pos_end = mb_strpos( $content_element, '"', $href_pos_start,'UTF-8' );
- //Находим title
- $img_url = mb_substr($content_element, $href_pos_start, $href_pos_end - $href_pos_start, 'UTF-8' );
- //Находим строки для замены
- if ($img_url !== "") {
- $alt_start += 5;
- $title_start += 7;
- $alt = mb_substr($content_element, $alt_start, $alt_end - $alt_start, 'UTF-8' );
- $title = mb_substr($content_element, $title_start, $title_end - $title_start, 'UTF-8' );
- //var_dump( $title );
- $content_element = str_replace($alt, $title, $content_element);
- }
- $new_content_arr[] = $content_element;
- }
- $content = implode( '[/caption]', $new_content_arr );
- //if ( $id == 1005 ){
- try {
- echo '>>>>>Начало<<<<<<<' . PHP_EOL;
- $sql = "UPDATE `{$table_prefix}posts` as `posts`
- SET `posts`.`post_content` = ?
- WHERE `posts`.`ID` = ?";
- $statement = $db->prepare( $sql );
- $statement->execute([$content, $id]);
- echo 'id = '.$id.' >>>>>Обновлено<<<<<<<' . PHP_EOL;
- } catch (\Exception $e) {
- if ($db->inTransaction()) {
- $db->rollback();
- // If we got here our data updates are not in the database
- }
- echo '>>>>>Ошибке<<<<<<< ' . PHP_EOL . $e->getMessage() . PHP_EOL;
- throw $e;
- }
- //}
- }
- $sql = "SELECT * FROM `{$table_prefix}posts` as `posts`
- WHERE `posts`.`ID`> {$id}
- AND `posts`.`post_content` LIKE '%[caption%]%title=%src=%[/caption]%'
- AND `posts`.`post_type` = 'post'
- ORDER BY `posts`.`ID` LIMIT 50";
- $statement = $db->prepare($sql);
- // Create some parameters to fill the placeholders, and execute the statement
- $parameters = [ "221B" ];
- $statement->execute($parameters);
- $rows = null;
- $rows = $statement->fetchAll(PDO::FETCH_OBJ);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement